Skip to content

Commit

Permalink
Add uid logging to tls_handshakes (#1233)
Browse files Browse the repository at this point in the history
Upstreams https://r.android.com/2759365

This reverts commit 3a1331299cbcf99e9af7e069b95793fa6f536b3f.

Reason for revert: try 3 of making this work (now with api level checking to avoid calling code that is not in R or S)

Co-authored-by: Miguel Aranda <miguelaranda@google.com>
  • Loading branch information
prbprbprb and miguelaranda0 authored Sep 18, 2024
1 parent 6b6b37a commit 7e3df46
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
4 changes: 4 additions & 0 deletions android-stub/src/main/java/android/util/StatsEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public StatsEvent.Builder writeInt(int value) {
throw new RuntimeException("Stub!");
}

public StatsEvent.Builder writeIntArray(int[] values) {

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).

Check failure on line 72 in android-stub/src/main/java/android/util/StatsEvent.java

View workflow job for this annotation

GitHub Actions / build (windows-latest)

warning: [DoNotCallSuggester] Methods that always throw an exception should be annotated with @DonotCall to prevent calls at compilation time vs. at runtime (note that adding @DonotCall will break any existing callers of this API).
throw new RuntimeException("Stub!");
}

public StatsEvent.Builder writeLong(long value) {
throw new RuntimeException("Stub!");
}
Expand Down
4 changes: 3 additions & 1 deletion android/src/main/java/org/conscrypt/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Binder;
import android.os.Build;
import android.os.SystemClock;
import android.system.Os;
import android.util.Log;
import dalvik.system.BlockGuard;
import dalvik.system.CloseGuard;
Expand Down Expand Up @@ -945,7 +947,7 @@ static void countTlsHandshake(
private static void writeStats(
boolean success, int protocol, int cipherSuite, int duration) {
ConscryptStatsLog.write(ConscryptStatsLog.TLS_HANDSHAKE_REPORTED, success, protocol,
cipherSuite, duration, SOURCE_GMS);
cipherSuite, duration, SOURCE_GMS, new int[] {Os.getuid(), Binder.getCallingUid()});
}

public static boolean isJavaxCertificateSupported() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public final class ConscryptStatsLog {

private ConscryptStatsLog() {}

public static void write(
int atomId, boolean success, int protocol, int cipherSuite, int duration, Source source) {
public static void write(int atomId, boolean success, int protocol, int cipherSuite,
int duration, Source source, int[] uids) {
ReflexiveStatsEvent event = ReflexiveStatsEvent.buildEvent(
atomId, success, protocol, cipherSuite, duration, source.ordinal());
atomId, success, protocol, cipherSuite, duration, source.ordinal(), uids);

ReflexiveStatsLog.write(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
public class ReflexiveStatsEvent {
private static final OptionalMethod newBuilder;
private static final Class<?> c_statsEvent;
private static final Object sdkVersion;
private static final boolean sdkVersionBiggerThan32;

static {
sdkVersion = getSdkVersion();
c_statsEvent = initStatsEventClass();
newBuilder = new OptionalMethod(c_statsEvent, "newBuilder");
sdkVersionBiggerThan32 = (sdkVersion != null) && ((int) sdkVersion > 32);
}

private static Class<?> initStatsEventClass() {
Expand All @@ -52,26 +56,55 @@ public static ReflexiveStatsEvent.Builder newBuilder() {
return new ReflexiveStatsEvent.Builder();
}

public static ReflexiveStatsEvent buildEvent(
int atomId, boolean success, int protocol, int cipherSuite, int duration, int source) {
public static ReflexiveStatsEvent buildEvent(int atomId, boolean success, int protocol,
int cipherSuite, int duration, int source, int[] uids) {
ReflexiveStatsEvent.Builder builder = ReflexiveStatsEvent.newBuilder();
builder.setAtomId(atomId);
builder.writeBoolean(success);
builder.writeInt(protocol);
builder.writeInt(cipherSuite);
builder.writeInt(duration);
builder.writeInt(source);
if (sdkVersionBiggerThan32) {
builder.writeIntArray(uids);
}
builder.usePooledBuffer();
return builder.build();
}

public static ReflexiveStatsEvent buildEvent(int atomId, boolean success, int protocol,
int cipherSuite, int duration, int source) {
ReflexiveStatsEvent.Builder builder = ReflexiveStatsEvent.newBuilder();
builder.setAtomId(atomId);
builder.writeBoolean(success);
builder.writeInt(protocol);
builder.writeInt(cipherSuite);
builder.writeInt(duration);
builder.writeInt(source);
builder.usePooledBuffer();
return builder.build();
}


static Object getSdkVersion() {
try {
OptionalMethod getSdkVersion =
new OptionalMethod(Class.forName("dalvik.system.VMRuntime"),
"getSdkVersion");
return getSdkVersion.invokeStatic();
} catch (ClassNotFoundException e) {
return null;
}
}

public static final class Builder {
private static final Class<?> c_statsEvent_Builder;
private static final OptionalMethod setAtomId;
private static final OptionalMethod writeBoolean;
private static final OptionalMethod writeInt;
private static final OptionalMethod build;
private static final OptionalMethod usePooledBuffer;
private static final OptionalMethod writeIntArray;

static {
c_statsEvent_Builder = initStatsEventBuilderClass();
Expand All @@ -80,6 +113,7 @@ public static final class Builder {
writeInt = new OptionalMethod(c_statsEvent_Builder, "writeInt", int.class);
build = new OptionalMethod(c_statsEvent_Builder, "build");
usePooledBuffer = new OptionalMethod(c_statsEvent_Builder, "usePooledBuffer");
writeIntArray = new OptionalMethod(c_statsEvent_Builder, "writeIntArray", int[].class);
}

private static Class<?> initStatsEventBuilderClass() {
Expand Down Expand Up @@ -115,6 +149,11 @@ public void usePooledBuffer() {
usePooledBuffer.invoke(this.builder);
}

public Builder writeIntArray(final int[] values) {
writeIntArray.invoke(this.builder, values);
return this;
}

public ReflexiveStatsEvent build() {
Object statsEvent = build.invoke(this.builder);
return new ReflexiveStatsEvent(statsEvent);
Expand Down
3 changes: 2 additions & 1 deletion platform/src/main/java/org/conscrypt/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ static void countTlsHandshake(
int duration = (int) durationLong;

ConscryptStatsLog.write(ConscryptStatsLog.TLS_HANDSHAKE_REPORTED, success, proto.getId(),
suite.getId(), duration, SOURCE_MAINLINE);
suite.getId(), duration, SOURCE_MAINLINE,
new int[] {Os.getuid()});
}

public static boolean isJavaxCertificateSupported() {
Expand Down
37 changes: 34 additions & 3 deletions platform/src/test/java/org/conscrypt/metrics/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.conscrypt.metrics.OptionalMethod;


@RunWith(JUnit4.class)
Expand All @@ -34,18 +35,36 @@ public class MetricsTest {
public void test_reflexiveEvent() throws Exception {
TestUtils.assumeStatsLogAvailable();

StatsEvent frameworkStatsEvent = StatsEvent.newBuilder()
Object sdkVersion = getSdkVersion();
StatsEvent frameworkStatsEvent;
ReflexiveStatsEvent reflexiveStatsEvent;
if ((sdkVersion != null) && ((int) sdkVersion > 32)) {
frameworkStatsEvent = StatsEvent.newBuilder()
.setAtomId(TLS_HANDSHAKE_REPORTED)
.writeBoolean(false)
.writeInt(1) // protocol
.writeInt(2) // cipher suite
.writeInt(100) // duration
.writeInt(3) // source
.writeIntArray(new int[] {0}) // uids
.usePooledBuffer()
.build();
reflexiveStatsEvent = ReflexiveStatsEvent.buildEvent(
TLS_HANDSHAKE_REPORTED, false, 1, 2, 100, 3, new int[] {0});
} else {
frameworkStatsEvent = StatsEvent.newBuilder()
.setAtomId(TLS_HANDSHAKE_REPORTED)
.writeBoolean(false)
.writeInt(1) // protocol
.writeInt(2) // cipher suite
.writeInt(100) // duration
.writeInt(3) // source
.usePooledBuffer()
.build();
reflexiveStatsEvent = ReflexiveStatsEvent.buildEvent(
TLS_HANDSHAKE_REPORTED, false, 1, 2, 100, 3);
}

ReflexiveStatsEvent reflexiveStatsEvent =
ReflexiveStatsEvent.buildEvent(TLS_HANDSHAKE_REPORTED, false, 1, 2, 100, 3);
StatsEvent constructedEvent = (StatsEvent) reflexiveStatsEvent.getStatsEvent();

// TODO(nikitai): Figure out how to use hidden (@hide) getters from StatsEvent
Expand Down Expand Up @@ -78,4 +97,16 @@ public void test_reflexiveEvent() throws Exception {
}
}
}

static Object getSdkVersion() {
try {
OptionalMethod getSdkVersion =
new OptionalMethod(Class.forName("dalvik.system.VMRuntime"),
"getSdkVersion");
return getSdkVersion.invokeStatic();
} catch (ClassNotFoundException e) {
return null;
}
}

}

0 comments on commit 7e3df46

Please sign in to comment.