-
Notifications
You must be signed in to change notification settings - Fork 564
[tools] initial version of an MSBuild "fuzzer" #3229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,327 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using Xamarin.ProjectTools; | ||
|
|
||
| namespace MSBuild.Fuzzer | ||
| { | ||
| class Program | ||
| { | ||
| static readonly Random random = new Random (); | ||
| static readonly List<string> installedPackages = new List<string> (); | ||
| static ProjectBuilder builder; | ||
| static XamarinFormsAndroidApplicationProject application; | ||
| static bool needsInstall = true; | ||
| static string directory; | ||
|
|
||
| static void Main () | ||
| { | ||
| var temp = Path.Combine (Path.GetDirectoryName (typeof (Program).Assembly.Location)); | ||
| using (builder = new ProjectBuilder (Path.Combine ("temp", "Fuzzer")) { | ||
| AutomaticNuGetRestore = false, | ||
| CleanupAfterSuccessfulBuild = false, | ||
| CleanupOnDispose = true, | ||
| Root = Xamarin.Android.Build.Paths.TestOutputDirectory, | ||
| }) { | ||
| directory = Path.GetFullPath (Path.Combine (builder.Root, builder.ProjectDirectory)); | ||
| if (Directory.Exists (directory)) | ||
| Directory.Delete (directory, recursive: true); | ||
|
|
||
| application = new XamarinFormsAndroidApplicationProject (); | ||
| application.AndroidManifest = application.AndroidManifest.Replace ("<uses-sdk />", "<uses-sdk android:targetSdkVersion=\"28\" />"); | ||
| application.MainActivity = application.DefaultMainActivity.Replace ("//${AFTER_ONCREATE}", "Android.Util.Log.Debug (\"FUZZER\", \"App started!\");"); | ||
| var abis = new string [] { "armeabi-v7a", "arm64-v8a", "x86" }; | ||
| application.SetProperty (KnownProperties.AndroidSupportedAbis, string.Join (";", abis)); | ||
|
|
||
| if (!NuGetRestore ()) { | ||
| Console.WriteLine ("Initial NuGet restore failed!"); | ||
| return; | ||
| } | ||
|
|
||
| Func<bool> [] operations = { | ||
| AddClass, | ||
| AddResource, | ||
| Build, | ||
| ChangePackageName, | ||
| Clean, | ||
| DesignerBuild, | ||
| DesignTimeBuild, | ||
| Install, | ||
| NuGetRestore, | ||
| RemoveClass, | ||
| RemoveResource, | ||
| RenameClass, | ||
| RenameResource, | ||
dellis1972 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Run, | ||
| TouchRandomFile, | ||
| Uninstall, | ||
| }; | ||
|
|
||
| while (true) { | ||
| var operation = operations [random.Next (operations.Length)]; | ||
| if (!operation ()) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Console.WriteLine ("Press enter to exit..."); | ||
| Console.ReadLine (); | ||
| } | ||
|
|
||
| static bool NuGetRestore () | ||
| { | ||
| Console.WriteLine (nameof (NuGetRestore)); | ||
| return builder.RunTarget (application, "Restore", doNotCleanupOnUpdate: true); | ||
| } | ||
|
|
||
| static bool Build () | ||
| { | ||
| Console.WriteLine (nameof (Build)); | ||
| return builder.Build (application, doNotCleanupOnUpdate: true); | ||
| } | ||
|
|
||
| static bool DesignTimeBuild () | ||
| { | ||
| Console.WriteLine (nameof (DesignTimeBuild)); | ||
| return builder.DesignTimeBuild (application, doNotCleanupOnUpdate: true); | ||
| } | ||
|
|
||
| static readonly string [] DesignerParameters = new [] { "DesignTimeBuild=True", "AndroidUseManagedDesignTimeResourceGenerator=False" }; | ||
|
|
||
| static bool DesignerBuild () | ||
| { | ||
| Console.WriteLine (nameof (DesignerBuild)); | ||
| return builder.RunTarget (application, "SetupDependenciesForDesigner", doNotCleanupOnUpdate: true, parameters: DesignerParameters); | ||
| } | ||
|
|
||
| static bool Clean () | ||
| { | ||
| Console.WriteLine (nameof (Clean)); | ||
| return builder.Clean (application, doNotCleanupOnUpdate: true); | ||
| } | ||
|
|
||
| static bool Install () | ||
| { | ||
| Console.WriteLine (nameof (Install)); | ||
| if (!builder.Install (application, doNotCleanupOnUpdate: true)) { | ||
| return false; | ||
| } | ||
| if (!installedPackages.Contains (application.PackageName)) | ||
| installedPackages.Add (application.PackageName); | ||
| needsInstall = false; | ||
| return true; | ||
| } | ||
|
|
||
| static bool Uninstall () | ||
| { | ||
| Console.WriteLine (nameof (Uninstall)); | ||
| foreach (var packageName in installedPackages) { | ||
jonathanpeppers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Adb ($"uninstall {packageName}"); | ||
| } | ||
| installedPackages.Clear (); | ||
| needsInstall = true; | ||
| return true; | ||
| } | ||
|
|
||
| static bool Run () | ||
| { | ||
| if (needsInstall && !Install ()) | ||
| return false; | ||
| Console.WriteLine (nameof (Run)); | ||
| string stop = Adb ($"shell am force-stop {application.PackageName}", ignoreExitCode: true); | ||
| Adb ("logcat -c"); | ||
| string activity = $"{application.PackageName}/md52d9cf6333b8e95e8683a477bc589eda5.MainActivity"; | ||
| string start = Adb ($"shell am start -n {activity}"); | ||
| Console.WriteLine (start.Trim ()); | ||
| //Wait for the app to start | ||
| Thread.Sleep (3000); | ||
| string logcat = Adb ("logcat -d"); | ||
| if (!logcat.Contains ("App started!")) { | ||
| Console.WriteLine (logcat); | ||
| throw new Exception ($"Activity {activity} did not start!"); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static string Adb (string arguments, bool ignoreExitCode = false) | ||
| { | ||
| var info = new ProcessStartInfo { | ||
| FileName = "adb", | ||
| Arguments = arguments, | ||
| CreateNoWindow = true, | ||
| WindowStyle = ProcessWindowStyle.Hidden, | ||
| UseShellExecute = false, | ||
| RedirectStandardError = true, | ||
| RedirectStandardOutput = true, | ||
| }; | ||
| var builder = new StringBuilder (); | ||
| using (var process = new Process ()) { | ||
| var stdout_done = new ManualResetEventSlim (false); | ||
| var stderr_done = new ManualResetEventSlim (false); | ||
| process.StartInfo = info; | ||
| process.OutputDataReceived += (sender, e) => { | ||
| if (e.Data != null) { | ||
| builder.AppendLine (e.Data); | ||
| } else { | ||
| stdout_done.Set (); | ||
| } | ||
| }; | ||
| process.ErrorDataReceived += (sender, e) => { | ||
| if (e.Data != null) { | ||
| builder.AppendLine (e.Data); | ||
| } else { | ||
| stderr_done.Set (); | ||
| } | ||
| }; | ||
| process.Start (); | ||
| process.BeginErrorReadLine (); | ||
| process.BeginOutputReadLine (); | ||
| process.WaitForExit (); | ||
| stderr_done.Wait (); | ||
| stdout_done.Wait (); | ||
| if (!ignoreExitCode && process.ExitCode != 0) { | ||
jonathanpeppers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine (builder); | ||
| throw new Exception ($"Adb exited with code: {process.ExitCode}"); | ||
| } | ||
| } | ||
| return builder.ToString (); | ||
| } | ||
|
|
||
| static readonly string [] extensions = { | ||
| ".cs", | ||
| ".csproj", | ||
| ".png", | ||
| ".xaml", | ||
| ".xml", | ||
| }; | ||
|
|
||
| static bool TouchRandomFile () | ||
| { | ||
| Console.WriteLine (nameof (TouchRandomFile)); | ||
| var files = (from f in Directory.EnumerateFiles (directory, "*", SearchOption.AllDirectories) | ||
| let relative = f.Substring (directory.Length + 1) | ||
| where !relative.StartsWith ("obj") && !relative.StartsWith ("bin") | ||
| let ext = Path.GetExtension (f) | ||
| where extensions.Contains (ext) | ||
| select f).ToArray (); | ||
| var file = files [random.Next (0, files.Length)]; | ||
| File.SetLastWriteTimeUtc (file, DateTime.UtcNow); | ||
| File.SetLastAccessTimeUtc (file, DateTime.UtcNow); | ||
| return true; | ||
| } | ||
|
|
||
| static bool ChangePackageName () | ||
| { | ||
| Console.WriteLine (nameof (ChangePackageName)); | ||
| application.PackageName = "com.foo.a" + RandomName (); | ||
| application.Touch ("Properties\\AndroidManifest.xml"); | ||
| needsInstall = true; | ||
| return true; | ||
| } | ||
|
|
||
| static bool AddClass () | ||
| { | ||
| Console.WriteLine (nameof (AddClass)); | ||
| application.Sources.Add (new Class ()); | ||
| return true; | ||
| } | ||
|
|
||
| static bool RemoveClass () | ||
| { | ||
| Console.WriteLine (nameof (RemoveClass)); | ||
| for (int i = application.Sources.Count - 1; i >= 0; i--) { | ||
| if (application.Sources [i] is Class) { | ||
| application.Sources.RemoveAt (i); | ||
| break; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static bool RenameClass () | ||
| { | ||
| Console.WriteLine (nameof (RemoveClass)); | ||
| var clazz = application.Sources.OfType<Class> ().FirstOrDefault (); | ||
| if (clazz != null) { | ||
| clazz.Rename (); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static bool AddResource () | ||
| { | ||
| Console.WriteLine (nameof (AddResource)); | ||
| application.Sources.Add (new AndroidResource ()); | ||
| return true; | ||
| } | ||
|
|
||
| static bool RemoveResource () | ||
| { | ||
| Console.WriteLine (nameof (RemoveResource)); | ||
| for (int i = application.Sources.Count - 1; i >= 0; i--) { | ||
| if (application.Sources [i] is AndroidResource) { | ||
| application.Sources.RemoveAt (i); | ||
| break; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static bool RenameResource () | ||
| { | ||
| Console.WriteLine (nameof (RenameResource)); | ||
| var resource = application.Sources.OfType<AndroidResource> ().FirstOrDefault (); | ||
| if (resource != null) { | ||
| resource.Rename (); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| static string RandomName () => Guid.NewGuid ().ToString ("N"); | ||
|
|
||
| class Class : BuildItem.Source | ||
| { | ||
| public string TypeName { get; set; } | ||
|
|
||
| public Class () : base (RandomName () + ".cs") | ||
| { | ||
| Rename (); | ||
| TextContent = () => $"public class Foo{TypeName} : Java.Lang.Object {{ }}"; | ||
| } | ||
|
|
||
| public void Rename () | ||
| { | ||
| TypeName = RandomName (); | ||
| Timestamp = null; | ||
| } | ||
| } | ||
|
|
||
| class AndroidResource : BuildItem | ||
| { | ||
| public string ResourceId { get; set; } | ||
|
|
||
| public string StringValue { get; set; } | ||
|
|
||
| public AndroidResource () : base ("AndroidResource", RandomName () + ".xml") | ||
| { | ||
| Rename (); | ||
| TextContent = () => $@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
| <resources> | ||
| <string name=""foo_{ResourceId}"">{StringValue}</string> | ||
| </resources>"; | ||
| } | ||
|
|
||
| public void Rename () | ||
| { | ||
| ResourceId = RandomName (); | ||
| StringValue = RandomName (); | ||
| Timestamp = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net472</TargetFrameworks> | ||
| <ProjectGuid>{A3671983-ABC2-4693-8872-463427B57117}</ProjectGuid> | ||
| <OutputType>Exe</OutputType> | ||
| </PropertyGroup> | ||
| <Import Project="..\..\Configuration.props" /> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\src\Xamarin.Android.Build.Tasks\Tests\Xamarin.ProjectTools\Xamarin.ProjectTools.csproj"> | ||
| <Project>{2dd1ee75-6d8d-4653-a800-0a24367f7f38}</Project> | ||
| <Name>Xamarin.ProjectTools</Name> | ||
| </ProjectReference> | ||
| </ItemGroup> | ||
| </Project> | ||
jonathanpeppers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 16 | ||
| VisualStudioVersion = 16.0.28803.352 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msbuild-fuzzer", "msbuild-fuzzer.csproj", "{A3671983-ABC2-4693-8872-463427B57117}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.ProjectTools", "..\..\src\Xamarin.Android.Build.Tasks\Tests\Xamarin.ProjectTools\Xamarin.ProjectTools.csproj", "{2DD1EE75-6D8D-4653-A800-0A24367F7F38}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {A3671983-ABC2-4693-8872-463427B57117}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {A3671983-ABC2-4693-8872-463427B57117}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {A3671983-ABC2-4693-8872-463427B57117}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {A3671983-ABC2-4693-8872-463427B57117}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {2DD1EE75-6D8D-4653-A800-0A24367F7F38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {2DD1EE75-6D8D-4653-A800-0A24367F7F38}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {2DD1EE75-6D8D-4653-A800-0A24367F7F38}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {2DD1EE75-6D8D-4653-A800-0A24367F7F38}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {E88EED5D-9DFA-405D-9495-4EC9A146B249} | ||
| EndGlobalSection | ||
| EndGlobal |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.