Skip to content

Commit 1bf1a91

Browse files
committed
Moving on with reporters
1 parent e381acf commit 1bf1a91

11 files changed

+196
-117
lines changed

tools/apput/src/Common/AspectReporterAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ApplicationUtility;
44

5-
[AttributeUsage (AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
5+
[AttributeUsage (AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
66
class AspectReporterAttribute : Attribute
77
{
88
public Type AspectType { get; }

tools/apput/src/Detector.cs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ public class Detector
1515
// Aspects must be listed in the order of detection, from the biggest (the most generic) to the
1616
// smallest (the least generic) aspects.
1717
public readonly static List<Type> KnownTopLevelAspects = new () {
18-
typeof (ApplicationPackage),
18+
typeof (PackageAPK),
19+
typeof (PackageAAB),
20+
typeof (PackageBase),
1921
typeof (AssemblyStore),
2022
typeof (ApplicationAssembly),
2123
typeof (NativeAotSharedLibrary),
22-
typeof (LibXamarinApp),
24+
typeof (XamarinAppSharedLibrary),
2325
typeof (SharedLibrary),
2426
};
2527

2628
readonly static List<Type> KnownSharedLibraryAspects = new () {
2729
typeof (NativeAotSharedLibrary),
28-
typeof (LibXamarinApp),
30+
typeof (XamarinAppSharedLibrary),
2931
typeof (SharedLibrary),
3032
};
3133

@@ -49,44 +51,53 @@ public class Detector
4951
public static SharedLibrary? FindSharedLibraryAspect (Stream stream, string? description = null)
5052
{
5153
Log.Debug ($"Looking for shared library aspect ('{description}')");
52-
// TODO: implement
54+
return (SharedLibrary?)TryFindAspect (KnownSharedLibraryAspects, stream, description);
55+
}
56+
57+
static IAspect? TryFindTopLevelAspect (Stream stream, string? description) => TryFindAspect (KnownTopLevelAspects, stream, description);
58+
59+
static IAspect? TryFindAspect (List<Type> aspectTypes, Stream stream, string? description)
60+
{
61+
foreach (Type aspectType in aspectTypes) {
62+
IAspect? aspect = TryProbeAndLoadAspect (aspectType, stream, description);
63+
if (aspect != null) {
64+
return aspect;
65+
}
66+
}
67+
5368
return null;
5469
}
5570

56-
static IAspect? TryFindTopLevelAspect (Stream stream, string? description)
71+
static IAspect? TryProbeAndLoadAspect (Type aspect, Stream stream, string? description)
5772
{
58-
var flags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static;
73+
const BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
5974

60-
foreach (Type aspect in KnownTopLevelAspects) {
61-
LogBanner ($"Probing aspect: {aspect}");
75+
LogBanner ($"Probing aspect: {aspect}");
76+
object? result = aspect.InvokeMember (
77+
"ProbeAspect", flags, null, null, new object?[] { stream, description }
78+
);
6279

63-
object? result = aspect.InvokeMember (
64-
"ProbeAspect", flags, null, null, new object?[] { stream, description }
65-
);
80+
var state = result as IAspectState;
81+
if (state == null || !state.Success) {
82+
return null;
83+
}
6684

67-
var state = result as IAspectState;
68-
if (state == null || !state.Success) {
69-
continue;
70-
}
85+
LogBanner ($"Loading aspect: {aspect}");
86+
result = aspect.InvokeMember (
87+
"LoadAspect", flags, null, null, new object?[] { stream, state, description }
88+
);
7189

72-
LogBanner ($"Loading aspect: {aspect}");
73-
result = aspect.InvokeMember (
74-
"LoadAspect", flags, null, null, new object?[] { stream, state, description }
75-
);
76-
if (result != null) {
77-
return (IAspect)result;
78-
}
90+
if (result != null) {
91+
return (IAspect)result;
7992
}
8093

8194
return null;
8295

8396
void LogBanner (string what)
8497
{
8598
Log.Debug ();
86-
Log.Debug ("##########");
87-
Log.Debug (what);
99+
Log.Debug ($"# {what}");
88100
Log.Debug ();
89101
}
90102
}
91-
92103
}

tools/apput/src/Native/AnELF.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public static bool TryLoad (string filePath, out AnELF? anElf)
242242
public static bool TryLoad (Stream stream, string filePath, out AnELF? anElf)
243243
{
244244
anElf = null;
245+
stream.Seek (0, SeekOrigin.Begin);
245246
Class elfClass = ELFReader.CheckELFType (stream);
246247
if (elfClass == Class.NotELF) {
247248
Log.Warning ($"AnELF.TryLoad: {filePath} is not an ELF binary");

tools/apput/src/Native/LibXamarinApp.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

tools/apput/src/Native/NativeAotSharedLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected NativeAotSharedLibrary (Stream stream, string libraryName)
3535

3636
public new static IAspectState ProbeAspect (Stream stream, string? description) => new BasicAspectState (IsNativeAotSharedLibrary (stream, description));
3737

38-
static bool IsNativeAotSharedLibrary (Stream stream, string description)
38+
static bool IsNativeAotSharedLibrary (Stream stream, string? description)
3939
{
4040
if (!IsSupportedELFSharedLibrary (stream, description, out IELF? elf) || elf == null) {
4141
return false;

tools/apput/src/Native/NativeAppInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace ApplicationUtility;
22

33
public class NativeAppInfo
44
{
5-
internal NativeAppInfo (LibXamarinApp xamarinAppLibrary)
5+
internal NativeAppInfo (XamarinAppSharedLibrary xamarinAppLibrary)
66
{
77
}
88
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ApplicationUtility;
2+
3+
class XamarinAppLibraryAspectState : BasicAspectState
4+
{
5+
public ulong FormatTag { get; }
6+
7+
public XamarinAppLibraryAspectState (bool success, ulong formatTag)
8+
: base (success)
9+
{
10+
FormatTag = formatTag;
11+
}
12+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.IO;
3+
4+
using ELFSharp.ELF;
5+
6+
namespace ApplicationUtility;
7+
8+
// TODO: make it an abstract class, we need to support different formats
9+
class XamarinAppSharedLibrary : SharedLibrary
10+
{
11+
const string FormatTagSymbol = "format_tag";
12+
13+
public ulong FormatTag { get; }
14+
15+
XamarinAppSharedLibrary (Stream stream, string description, XamarinAppLibraryAspectState state)
16+
: base (stream, description)
17+
{
18+
FormatTag = state.FormatTag;
19+
}
20+
21+
public static new IAspect LoadAspect (Stream stream, IAspectState state, string? description)
22+
{
23+
if (String.IsNullOrEmpty (description)) {
24+
throw new ArgumentException ("Must be a shared library name", nameof (description));
25+
}
26+
27+
if (!IsSupportedELFSharedLibrary (stream, description)) {
28+
throw new InvalidOperationException ("Stream is not a supported ELF shared library");
29+
}
30+
31+
// TODO: this needs to be versioned
32+
return new XamarinAppSharedLibrary (stream, description, (XamarinAppLibraryAspectState)state);
33+
}
34+
35+
public static new IAspectState ProbeAspect (Stream stream, string? description) => IsXamarinAppSharedLibrary (stream, description);
36+
37+
static XamarinAppLibraryAspectState IsXamarinAppSharedLibrary (Stream stream, string? description)
38+
{
39+
if (!IsSupportedELFSharedLibrary (stream, description, out IELF? elf) || elf == null) {
40+
return GetErrorState ();
41+
}
42+
43+
if (!AnELF.TryLoad (stream, description ?? String.Empty, out AnELF? anElf) || anElf == null) {
44+
Log.Debug ($"Failed to load '{description}' as a Xamarin.Android application shared library");
45+
return GetErrorState ();
46+
}
47+
48+
if (!anElf.HasSymbol (FormatTagSymbol)) {
49+
return LogMissingSymbolAndReturn (FormatTagSymbol);
50+
}
51+
ulong formatTag = anElf.GetUInt64 (FormatTagSymbol);
52+
53+
// TODO: check for presence of a handful of fields more
54+
return GetState (success: true, formatTag);
55+
56+
XamarinAppLibraryAspectState LogMissingSymbolAndReturn (string name)
57+
{
58+
Log.Debug ($"{description} is not a Xamarin.Android application shared library, it doesn't have the '{name}' symbol.");
59+
return GetErrorState ();
60+
}
61+
62+
XamarinAppLibraryAspectState GetState (bool success, ulong formatTag) => new XamarinAppLibraryAspectState (success, formatTag);
63+
XamarinAppLibraryAspectState GetErrorState () => GetState (success: false, formatTag: 0);
64+
}
65+
}

0 commit comments

Comments
 (0)