Fix dvmHumanReadableDescriptor("B").

dvmHumanReadableDescriptor was failing for primitive scalars in
libdvm_assert.so --- there was an assertion that wasn't true,
because we wouldn't have copied the ';' into the result (in the
other cases, we'd copy the ';' and then overwrite it).

This patch rewrites dvmHumanReadableDescriptor to be a little
less tasteless, removes the asserts, behave better when out of
memory, and to include unit tests (currently only runnable on
the device).

Bug: 3245572
Change-Id: I57b06c774a5bc61c2d374cce6285553c3c5f388b
diff --git a/Android.mk b/Android.mk
index 7a950a1..8ab58e9 100644
--- a/Android.mk
+++ b/Android.mk
@@ -26,6 +26,7 @@
 		dx \
 		libnativehelper \
 		tools \
+		unit-tests \
 	))
 
 include $(subdirs)
diff --git a/unit-tests/Android.mk b/unit-tests/Android.mk
new file mode 100644
index 0000000..e32e890
--- /dev/null
+++ b/unit-tests/Android.mk
@@ -0,0 +1,44 @@
+#
+# Copyright (C) 2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# Copyright The Android Open Source Project
+
+LOCAL_PATH := $(call my-dir)
+
+test_module = dalvik-vm-unit-tests
+test_tags = eng tests
+
+test_src_files = \
+    dvmHumanReadableDescriptor_test.cpp \
+
+
+# Build for the device. Run with:
+#   adb shell /data/nativetest/dalvik-vm-unit-tests/dalvik-vm-unit-tests
+include $(CLEAR_VARS)
+LOCAL_MODULE := $(test_module)
+LOCAL_MODULE_TAGS := $(test_tags)
+LOCAL_SRC_FILES := $(test_src_files)
+LOCAL_SHARED_LIBRARIES += libcutils libutils libdvm
+include $(BUILD_NATIVE_TEST)
+
+# Build for the host.
+# TODO: BUILD_HOST_NATIVE_TEST doesn't work yet; STL-related compile-time and
+# run-time failures, presumably astl/stlport/genuine host STL confusion.
+#include $(CLEAR_VARS)
+#LOCAL_MODULE := $(test_module)
+#LOCAL_MODULE_TAGS := $(test_tags)
+#LOCAL_SRC_FILES := $(test_src_files)
+#LOCAL_SHARED_LIBRARIES += libdvm libcrypto libssl libicuuc libicui18n
+#LOCAL_WHOLE_STATIC_LIBRARIES += libcutils libutils liblog libdvm
+#include $(BUILD_HOST_NATIVE_TEST)
diff --git a/unit-tests/dvmHumanReadableDescriptor_test.cpp b/unit-tests/dvmHumanReadableDescriptor_test.cpp
new file mode 100644
index 0000000..8530df4
--- /dev/null
+++ b/unit-tests/dvmHumanReadableDescriptor_test.cpp
@@ -0,0 +1,44 @@
+#include <gtest/gtest.h>
+
+// TODO: make dalvik's .h files C++-clean.
+extern "C" char* dvmHumanReadableDescriptor(const char* descriptor);
+
+TEST(dvmHumanReadableDescriptor, ArrayReferences) {
+  ASSERT_STREQ("java.lang.Class[]", dvmHumanReadableDescriptor("[Ljava/lang/Class;"));
+  ASSERT_STREQ("java.lang.Class[][]", dvmHumanReadableDescriptor("[[Ljava/lang/Class;"));
+}
+
+TEST(dvmHumanReadableDescriptor, ScalarReferences) {
+  ASSERT_STREQ("java.lang.String", dvmHumanReadableDescriptor("Ljava.lang.String;"));
+  ASSERT_STREQ("java.lang.String", dvmHumanReadableDescriptor("Ljava/lang/String;"));
+}
+
+TEST(dvmHumanReadableDescriptor, PrimitiveArrays) {
+  ASSERT_STREQ("boolean[]", dvmHumanReadableDescriptor("[Z"));
+  ASSERT_STREQ("boolean[][]", dvmHumanReadableDescriptor("[[Z"));
+  ASSERT_STREQ("byte[]", dvmHumanReadableDescriptor("[B"));
+  ASSERT_STREQ("byte[][]", dvmHumanReadableDescriptor("[[B"));
+  ASSERT_STREQ("char[]", dvmHumanReadableDescriptor("[C"));
+  ASSERT_STREQ("char[][]", dvmHumanReadableDescriptor("[[C"));
+  ASSERT_STREQ("double[]", dvmHumanReadableDescriptor("[D"));
+  ASSERT_STREQ("double[][]", dvmHumanReadableDescriptor("[[D"));
+  ASSERT_STREQ("float[]", dvmHumanReadableDescriptor("[F"));
+  ASSERT_STREQ("float[][]", dvmHumanReadableDescriptor("[[F"));
+  ASSERT_STREQ("int[]", dvmHumanReadableDescriptor("[I"));
+  ASSERT_STREQ("int[][]", dvmHumanReadableDescriptor("[[I"));
+  ASSERT_STREQ("long[]", dvmHumanReadableDescriptor("[J"));
+  ASSERT_STREQ("long[][]", dvmHumanReadableDescriptor("[[J"));
+  ASSERT_STREQ("short[]", dvmHumanReadableDescriptor("[S"));
+  ASSERT_STREQ("short[][]", dvmHumanReadableDescriptor("[[S"));
+}
+
+TEST(dvmHumanReadableDescriptor, PrimitiveScalars) {
+  ASSERT_STREQ("boolean", dvmHumanReadableDescriptor("Z"));
+  ASSERT_STREQ("byte", dvmHumanReadableDescriptor("B"));
+  ASSERT_STREQ("char", dvmHumanReadableDescriptor("C"));
+  ASSERT_STREQ("double", dvmHumanReadableDescriptor("D"));
+  ASSERT_STREQ("float", dvmHumanReadableDescriptor("F"));
+  ASSERT_STREQ("int", dvmHumanReadableDescriptor("I"));
+  ASSERT_STREQ("long", dvmHumanReadableDescriptor("J"));
+  ASSERT_STREQ("short", dvmHumanReadableDescriptor("S"));
+}
diff --git a/vm/Misc.c b/vm/Misc.c
index f80ceb8..564d415 100644
--- a/vm/Misc.c
+++ b/vm/Misc.c
@@ -411,61 +411,62 @@
 
 char* dvmHumanReadableDescriptor(const char* descriptor)
 {
-    char *dotName = dvmDescriptorToDot(descriptor);
-    if (descriptor[0] == 'L') {
-        return dotName;
-    }
-
-    const char* c = dotName;
+    // Count the number of '['s to get the dimensionality.
+    const char* c = descriptor;
     size_t dim = 0;
     while (*c == '[') {
         dim++;
         c++;
     }
+
+    // Work out how large the result will be.
+    size_t resultLength;
     if (*c == 'L') {
-        c++;
+        // "[[La/b/C;" -> "a.b.C[][]".
+        resultLength = strlen(c) - 2 + 2*dim;
+        c++; // Skip the 'L'.
     } else {
-        /* It's a primitive type;  we should use a pretty name.
-         * Add semicolons to make all strings have the format
-         * of object class names.
-         */
+        // "[[B" -> "byte[][]".
+        // To make life easier, we make primitives look like unqualified
+        // reference types.
         switch (*c) {
-        case 'Z': c = "boolean;";    break;
-        case 'C': c = "char;";       break;
-        case 'F': c = "float;";      break;
-        case 'D': c = "double;";     break;
-        case 'B': c = "byte;";       break;
-        case 'S': c = "short;";      break;
-        case 'I': c = "int;";        break;
-        case 'J': c = "long;";       break;
-        default: assert(false); c = "UNKNOWN;"; break;
+        case 'B': c = "byte;"; break;
+        case 'C': c = "char;"; break;
+        case 'D': c = "double;"; break;
+        case 'F': c = "float;"; break;
+        case 'I': c = "int;"; break;
+        case 'J': c = "long;"; break;
+        case 'S': c = "short;"; break;
+        case 'Z': c = "boolean;"; break;
+        default: return strdup(descriptor);
         }
+        resultLength = strlen(c) - 1 + 2*dim;
     }
 
-    /* We have a string of the form "name;" and
-     * we want to replace the semicolon with as many
-     * "[]" pairs as is in dim.
-     */
-    size_t newLen = strlen(c)-1 + dim*2;
-    char* newName = malloc(newLen + 1);
-    if (newName == NULL) {
+    // Allocate enough space.
+    char* result = malloc(resultLength + 1);
+    if (result == NULL) {
         return NULL;
     }
-    strcpy(newName, c);
-    newName[newLen] = '\0';
 
-    /* Point nc to the semicolon.
-     */
-    char* nc = newName + newLen - dim*2;
-    assert(*nc == ';');
-
-    while (dim--) {
-        *nc++ = '[';
-        *nc++ = ']';
+    // At this point, 'c' is a string of the form "fully/qualified/Type;"
+    // or "primitive;". Rewrite the type with '.' instead of '/':
+    const char* p = c;
+    char* q = result;
+    while (*p != ';') {
+        char ch = *p++;
+        if (ch == '/') {
+          ch = '.';
+        }
+        *q++ = ch;
     }
-    assert(*nc == '\0');
-    free(dotName);
-    return newName;
+    // ...and replace the semicolon with 'dim' "[]" pairs:
+    while (dim--) {
+        *q++ = '[';
+        *q++ = ']';
+    }
+    *q = '\0';
+    return result;
 }
 
 /*