﻿# Checking the Ark Compiler recently made open\-source by Huawei

During the summer of 2019, Huawei gave a series of presentations announcing the Ark Compiler technology\. The company claims that this open\-source project will help developers make the Android system and third\-party software much more fluent and responsive\. By tradition, every new promising open\-source project goes through PVS\-Studio for us to evaluate the quality of its code\.

![0690_OpenArkCompiler/image1.png](https://import.viva64.com/docx/blog/0690_OpenArkCompiler/image1.png)

## Introduction

The Ark Compiler was first announced by Huawei at the launch of the new smartphone models P30 and P30 Pro\. It is claimed that the Ark Compiler will improve the fluency of the Android system by 24% and response speed by 44%\. Third\-party Android applications will also gain a 60% speed\-up after recompilation with the Ark Compiler\. The open\-source version of the project is called OpenArkCompiler; its source code is available on [Gitee](https://gitee.com/openarkcompiler/OpenArkCompiler), a Chinese fork of GitHub\.

To check this project, I used the [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/) static code analyzer\. This is a tool for detecting bugs and potential vulnerabilities in the source code of C, C\+\+, C\#, and Java programs\.

The project's size is 50 KLOC and it didn't take long to check\. A small project means modest results: the article will be focusing on 11 out of the total of 39 warnings \(of High and Medium levels\)\.

## Defects found in the code

**Warning 1**

[V502](https://pvs-studio.com/en/docs/warnings/v502/) Perhaps the '?:' operator works in a different way than it was expected\. The '?:' operator has a lower priority than the '\=\=' operator\. mir\_parser\.cpp 884

```cpp
enum Opcode : uint8 {
  kOpUndef,
  ....
  OP_intrinsiccall,
  OP_intrinsiccallassigned,
  ....
  kOpLast,
};

bool MIRParser::ParseStmtIntrinsiccall(StmtNodePtr &stmt, bool isAssigned) {
  Opcode o = !isAssigned ? (....)
                         : (....);
  auto *intrnCallNode = mod.CurFuncCodeMemPool()->New<IntrinsiccallNode>(....);
  lexer.NextToken();
  if (o == !isAssigned ? OP_intrinsiccall : OP_intrinsiccallassigned) {
    intrnCallNode->SetIntrinsic(GetIntrinsicID(lexer.GetTokenKind()));
  } else {
    intrnCallNode->SetIntrinsic(static_cast<MIRIntrinsicID>(....));
  }
  ....
}
```

We are interested in the following part:

```cpp
if (o == !isAssigned ? OP_intrinsiccall : OP_intrinsiccallassigned) {
  ....
}
```

The precedence of the '\=\=' operator is higher than that of the ternary operator \(?:\)\. Therefore, the conditional expression is evaluated in the wrong order and is equivalent to the following code:

```cpp
if ((o == !isAssigned) ? OP_intrinsiccall : OP_intrinsiccallassigned) {
  ....
}
```

Since the constants _OP\_intrinsiccall_ and _OP\_intrinsiccallassigned_ are non\-null, the condition will be returning _true _all the time, which means the body of the _else_ branch is unreachable code\.

**Warning 2**

[V570](https://pvs-studio.com/en/docs/warnings/v570/) The 'theDoubleVal' variable is assigned to itself\. lexer\.cpp 283

```cpp
int64 theIntVal = 0;
float theFloatVal = 0.0;
double theDoubleVal = 0.0;

TokenKind MIRLexer
::GetFloatConst(uint32 valStart, uint32 startIdx, bool negative) {
  ....
  theIntVal = static_cast<int>(theFloatVal);
  theDoubleVal = static_cast<double>(theDoubleVal); // <=
  if (theFloatVal == -0) {
    theDoubleVal = -theDoubleVal;
  }
  ....
}
```

The _theDoubleVal_ variable is assigned to itself without changing\. The developer must have intended to store the result to _theFloatVal_ instead because it is this variable that gets checked in the next line\. If so, it should also be cast to _float_, not _double_\. I think the fixed version should look like this:

```cpp
theFloatVal = static_cast<float>(theDoubleVal);
if (theFloatVal == -0) {
  theDoubleVal = -theDoubleVal;
```

or even like this if the programmer simply wrote the wrong variable in the condition:

```cpp
if (theDoubleVal == -0) {
  theDoubleVal = -theDoubleVal;
```

I may still be wrong; perhaps this code should be fixed in some entirely different way\. It does look obscure to an outside programmer like myself\.

**Warnings 3\-5**

[V524](https://pvs-studio.com/en/docs/warnings/v524/) It is odd that the body of '\-' function is fully equivalent to the body of '\+' function\. mpl\_number\.h 158

```cpp
template <typename T, typename Tag>
inline Number<T, Tag> operator+(const Number<T, Tag> &lhs,
                                const Number<T, Tag> &rhs) {
  return Number<T, Tag>(lhs.get() + rhs.get());
}

template <typename T, typename Tag>
inline Number<T, Tag> operator-(const Number<T, Tag> &lhs,
                                const Number<T, Tag> &rhs) {
  return Number<T, Tag>(lhs.get() + rhs.get());
}
```

The header file _mpl\_number\.h_ contains a lot of duplicate code with small modifications – and mistakes, of course\. In this example, the addition and subtraction operators are implemented in the same way: the programmer forgot to change the operation sign in the body of the subtraction operator\.

Other warnings of this type:

* V524 It is odd that the body of '\-' function is fully equivalent to the body of '\+' function\. mpl\_number\.h 233
* V524 It is odd that the body of '\-' function is fully equivalent to the body of '\+' function\. mpl\_number\.h 238

**Warning 6**

[V560](https://pvs-studio.com/en/docs/warnings/v560/) A part of conditional expression is always false: \!firstImport\. parser\.cpp 2633

```cpp
bool MIRParser::ParseMIRForImport() {
  ....
  if (paramIsIPA && firstImport) {
    BinaryMplt *binMplt = new BinaryMplt(mod);
    mod.SetBinMplt(binMplt);
    if (!(*binMplt).Import(...., paramIsIPA && !firstImport, paramIsComb)) {
      ....
    }
    ....
  }
  ....
}
```

The _firstImport_ variable checked in the first conditional expression is always _true_\. It means the following expression will always evaluate to _false_:

```cpp
paramIsIPA && !firstImport
```

This code either contains a logic error or is overcomplicated and can be simplified by passing the _false_ constant to the _Import_ function\.

**Warning 7**

[V547](https://pvs-studio.com/en/docs/warnings/v547/) Expression 'idx \>\= 0' is always true\. Unsigned type value is always \>\= 0\. lexer\.h 129

```cpp
char GetCharAtWithLowerCheck(uint32 idx) const {
  return idx >= 0 ? line[idx] : 0;
}
```

This check of the index variable _idx_ \(\>\= 0\) doesn't make sense because the variable is unsigned\. Perhaps it was meant to be compared with some other value as the threshold for indexing into the _line_ array, or this meaningless check should be removed altogether\.

**Warning 8**

[V728](https://pvs-studio.com/en/docs/warnings/v728/) An excessive check can be simplified\. The '\|\|' operator is surrounded by opposite expressions 'c \!\= '\\"'' and 'c \=\= '\\"''\.  lexer\.cpp 400

```cpp
TokenKind MIRLexer::GetTokenWithPrefixDoubleQuotation() {
  ....
  char c = GetCurrentCharWithUpperCheck();
  while ((c != 0) &&
         (c != '\"' || (c == '\"' && GetCharAtWithLowerCheck(....) == '\\'))) {
    ....
  }
  ....
}
```

The analyzer has spotted a code pattern that can be simplified\. It looks similar to this form:

```cpp
A || (!A && smth)
```

The _\!A_ expression will always evaluate to _true_, so the original expression can be simplified as follows:

```cpp
while ((c != 0) && (c != '\"' || (GetCharAtWithLowerCheck(....) == '\\'))) {
  ....
}
```

**Warnings 9\-10**

[V728](https://pvs-studio.com/en/docs/warnings/v728/) An excessive check can be simplified\. The '\(A && \!B\) \|\| \(\!A && B\)' expression is equivalent to the 'bool\(A\) \!\= bool\(B\)' expression\. mir\_nodes\.cpp 1552

```cpp
bool BinaryNode::Verify() const {
  ....
  if ((IsAddress(GetBOpnd(0)->GetPrimType()) &&
      !IsAddress(GetBOpnd(1)->GetPrimType()))
    ||
     (!IsAddress(GetBOpnd(0)->GetPrimType()) &&
       IsAddress(GetBOpnd(1)->GetPrimType()))) {
    ....
  }
  ....
}
```

This is another snippet that needs refactoring\. To make it more readable, I split the code into several lines, while in its original form, the condition occupies two full lines, which made it much more difficult to figure out\. The code can be rewritten in a simpler and clearer form:

```cpp
if (IsAddress(GetBOpnd(0)->GetPrimType()) !=
    IsAddress(GetBOpnd(1)->GetPrimType()))
  ....
}
```

Another code fragment to be refactored in a similar way:

* V728 An excessive check can be simplified\. The '\(A && B\) \|\| \(\!A && \!B\)' expression is equivalent to the 'bool\(A\) \=\= bool\(B\)' expression\. bin\_mpl\_import\.cpp 702

**Warning 11**

[V1048](https://pvs-studio.com/en/docs/warnings/v1048/) The 'floatSpec\-\>floatStr' variable was assigned the same value\. input\.inl 1356

```cpp
static void SecInitFloatSpec(SecFloatSpec *floatSpec)
{
  floatSpec->floatStr = floatSpec->buffer;
  floatSpec->allocatedFloatStr = NULL;
  floatSpec->floatStrSize = sizeof(floatSpec->buffer) /
                            sizeof(floatSpec->buffer[0]);
  floatSpec->floatStr = floatSpec->buffer;
  floatSpec->floatStrUsedLen = 0;
}
```

The analyzer has detected two identical initializations of the variable _floatSpec\-\>floatStr_\. I believe the second duplicate line can be removed\.

## Conclusion

Just a few days ago, we checked another project by Huawei, [Huawei Cloud DIS SDK](https://pvs-studio.com/en/blog/posts/java/0688/)\. The company is currently making their projects open\-source, which is good news for the developer community\. Such projects as the Ark Compiler or Harmony OS are very young and haven't become popular yet, so investing in the quality control of the code at this stage should be very profitable since it can help avoid potential vulnerabilities and customer criticism\.

## References

1. [Checking LLVM, 2011](https://pvs-studio.com/en/blog/posts/0108/)
1. [Checking LLVM, 2012](https://pvs-studio.com/en/blog/posts/cpp/0155/)
1. [Checking GCC, 2016](https://pvs-studio.com/en/blog/posts/cpp/0425/)
1. [Checking LLVM, 2016](https://pvs-studio.com/en/blog/posts/cpp/0446/)
1. [Checking PascalABC\.NET, 2017](https://pvs-studio.com/en/blog/posts/csharp/0492/)
1. [Checking Roslyn \(\.NET Compiler Platform\), 2019](https://pvs-studio.com/en/blog/posts/csharp/0622/)
1. [Checking LLVM, 2019](https://pvs-studio.com/en/blog/posts/cpp/0629/)