diff options
| author | 2019-09-10 10:24:39 -0700 | |
|---|---|---|
| committer | 2019-09-10 10:24:39 -0700 | |
| commit | 5ab74cf7b5b15819b7b9f9c40d65bb826a87b85b (patch) | |
| tree | 1a8384d10bbe4137e132112f0c3a21b8730c4e23 | |
| parent | 2c4d0db5e8a95f38cbf7aae1e52bf6b2752bef1c (diff) | |
| parent | b8cce2152a0f09d564dc092eb0c259d228ee080f (diff) | |
Merge "Don't clear the output array if it failed to open the proc status file" am: 79a1b3a333 am: d939e46698
am: b8cce2152a
Change-Id: Id71a526c8b20b3413b2c398e582900c0a355be78
| -rw-r--r-- | core/jni/android_util_Process.cpp | 16 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/os/ProcessTest.java | 31 |
2 files changed, 38 insertions, 9 deletions
diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp index 2d7069c50255..f929dde4dcd1 100644 --- a/core/jni/android_util_Process.cpp +++ b/core/jni/android_util_Process.cpp @@ -644,6 +644,12 @@ static jlong android_os_Process_getTotalMemory(JNIEnv* env, jobject clazz) return si.totalram; } +/* + * The outFields array is initialized to -1 to allow the caller to identify + * when the status file (and therefore the process) they specified is invalid. + * This array should not be overwritten or cleared before we know that the + * status file can be read. + */ void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileStr, jobjectArray reqFields, jlongArray outFields) { @@ -692,14 +698,14 @@ void android_os_Process_readProcLines(JNIEnv* env, jobject clazz, jstring fileSt return; } - //ALOGI("Clearing %" PRId32 " sizes", count); - for (i=0; i<count; i++) { - sizesArray[i] = 0; - } - int fd = open(file.string(), O_RDONLY | O_CLOEXEC); if (fd >= 0) { + //ALOGI("Clearing %" PRId32 " sizes", count); + for (i=0; i<count; i++) { + sizesArray[i] = 0; + } + const size_t BUFFER_SIZE = 4096; char* buffer = (char*)malloc(BUFFER_SIZE); int len = read(fd, buffer, BUFFER_SIZE-1); diff --git a/core/tests/coretests/src/android/os/ProcessTest.java b/core/tests/coretests/src/android/os/ProcessTest.java index b749e715316a..6b02764b7f28 100644 --- a/core/tests/coretests/src/android/os/ProcessTest.java +++ b/core/tests/coretests/src/android/os/ProcessTest.java @@ -17,13 +17,12 @@ package android.os; -import androidx.test.filters.MediumTest; - import junit.framework.TestCase; public class ProcessTest extends TestCase { - @MediumTest + private static final int BAD_PID = 0; + public void testProcessGetUidFromName() throws Exception { assertEquals(android.os.Process.SYSTEM_UID, Process.getUidForName("system")); assertEquals(Process.BLUETOOTH_UID, Process.getUidForName("bluetooth")); @@ -35,7 +34,6 @@ public class ProcessTest extends TestCase { Process.getUidForName("u3_a100")); } - @MediumTest public void testProcessGetUidFromNameFailure() throws Exception { // Failure cases assertEquals(-1, Process.getUidForName("u2a_foo")); @@ -47,4 +45,29 @@ public class ProcessTest extends TestCase { assertEquals(-1, Process.getUidForName("u2jhsajhfkjhsafkhskafhkashfkjashfkjhaskjfdhakj3")); } + /** + * Tests getUidForPid() by ensuring that it returns the correct value when the process queried + * doesn't exist. + */ + public void testGetUidForPidInvalidPid() { + assertEquals(-1, Process.getUidForPid(BAD_PID)); + } + + /** + * Tests getParentPid() by ensuring that it returns the correct value when the process queried + * doesn't exist. + */ + public void testGetParentPidInvalidPid() { + assertEquals(-1, Process.getParentPid(BAD_PID)); + } + + /** + * Tests getThreadGroupLeader() by ensuring that it returns the correct value when the + * thread queried doesn't exist. + */ + public void testGetThreadGroupLeaderInvalidTid() { + // This function takes a TID instead of a PID but BAD_PID should also be a bad TID. + assertEquals(-1, Process.getThreadGroupLeader(BAD_PID)); + } + } |