Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Slight refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Porges authored May 16, 2023
1 parent c2c4277 commit c04872b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 189 deletions.
20 changes: 7 additions & 13 deletions src/ApiService/ApiService/OneFuzzTypes/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ public record VmDefinition(
long Value
);

public record TaskDefinition(
public readonly record struct TaskDefinition(
TaskFeature[] Features,
VmDefinition Vm,
ContainerDefinition[] Containers,
Expand All @@ -912,37 +912,31 @@ public record WorkSet(
List<WorkUnit> WorkUnits
);





public record ContainerDefinition(
public readonly record struct ContainerDefinition(
ContainerType Type,
Compare Compare,
long Value,
ContainerPermission Permissions);


// TODO: service shouldn't pass SyncedDir, but just the url and let the agent
// come up with paths
public record SyncedDir(string Path, Uri Url);

public readonly record struct SyncedDir(string Path, Uri Url);

[JsonConverter(typeof(ContainerDefConverter))]
public interface IContainerDef { }
public record SingleContainer(SyncedDir SyncedDir) : IContainerDef;
public record MultipleContainer(List<SyncedDir> SyncedDirs) : IContainerDef;
public record MultipleContainer(IReadOnlyList<SyncedDir> SyncedDirs) : IContainerDef;


public class ContainerDefConverter : JsonConverter<IContainerDef> {
public override IContainerDef? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
if (reader.TokenType == JsonTokenType.StartObject) {
var result = (SyncedDir?)JsonSerializer.Deserialize(ref reader, typeof(SyncedDir), options);
if (result is null) {
return null;
if (result is SyncedDir sd) {
return new SingleContainer(sd);
}

return new SingleContainer(result);
return null;
}

if (reader.TokenType == JsonTokenType.StartArray) {
Expand Down
142 changes: 72 additions & 70 deletions src/ApiService/ApiService/onefuzzlib/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,77 +78,79 @@ private static BlobContainerSasPermissions ConvertPermissions(ContainerPermissio
config.inputQueue = await _queue.GetQueueSas(task.TaskId.ToString(), StorageType.Corpus, QueueSasPermissions.Add | QueueSasPermissions.Read | QueueSasPermissions.Update | QueueSasPermissions.Process);
}

var containersByType = definition.Containers.Where(c => c.Type != ContainerType.Setup && task.Config.Containers != null)
.ToAsyncEnumerable()
.SelectAwait(async countainerDef => {
var containers = await
task.Config.Containers!
.Where(c => c.Type == countainerDef.Type).Select(container => (countainerDef, container))
.Where(x => x.container != null)
.ToAsyncEnumerable()
.SelectAwait(async (x, i) =>
new SyncedDir(
string.Join("_", "task", x.Item1.Type.ToString().ToLower(), i),
await _containers.GetContainerSasUrl(x.Item2.Name, StorageType.Corpus, ConvertPermissions(x.Item1.Permissions)))
).ToListAsync();
return (countainerDef, containers);
}
);

await foreach (var data in containersByType) {

if (!data.containers.Any()) {
continue;
}
if (task.Config.Containers is not null) {
var containersByType =
await Async.Task.WhenAll(
definition.Containers
.Where(c => c.Type is not ContainerType.Setup)
.Select(async countainerDef => {
var syncedDirs =
await Async.Task.WhenAll(
task.Config.Containers
.Where(c => c.Type == countainerDef.Type)
.Select(async (container, i) =>
new SyncedDir(
string.Join("_", "task", countainerDef.Type.ToString().ToLower(), i),
await _containers.GetContainerSasUrl(container.Name, StorageType.Corpus, ConvertPermissions(countainerDef.Permissions)))
));

return (countainerDef, syncedDirs);
}));

foreach (var (countainerDef, syncedDirs) in containersByType) {
if (!syncedDirs.Any()) {
continue;
}

IContainerDef def = data.countainerDef switch {
ContainerDefinition { Compare: Compare.Equal, Value: 1 } or
ContainerDefinition { Compare: Compare.AtMost, Value: 1 } when data.containers.Count == 1 => new SingleContainer(data.containers[0]),
_ => new MultipleContainer(data.containers)
};

switch (data.countainerDef.Type) {
case ContainerType.Analysis:
config.Analysis = def;
break;
case ContainerType.Coverage:
config.Coverage = def;
break;
case ContainerType.Crashes:
config.Crashes = def;
break;
case ContainerType.Inputs:
config.Inputs = def;
break;
case ContainerType.NoRepro:
config.NoRepro = def;
break;
case ContainerType.ReadonlyInputs:
config.ReadonlyInputs = def;
break;
case ContainerType.Reports:
config.Reports = def;
break;
case ContainerType.Tools:
config.Tools = def;
break;
case ContainerType.UniqueInputs:
config.UniqueInputs = def;
break;
case ContainerType.UniqueReports:
config.UniqueReports = def;
break;
case ContainerType.RegressionReports:
config.RegressionReports = def;
break;
case ContainerType.Extra:
config.Extra = def;
break;
case ContainerType.ExtraRw:
config.ExtraRw = def;
break;
default:
throw new InvalidDataException($"unknown container type: {data.countainerDef.Type}");
IContainerDef def = countainerDef switch {
ContainerDefinition { Compare: Compare.Equal | Compare.AtMost, Value: 1 }
when syncedDirs is [var syncedDir] => new SingleContainer(syncedDir),
_ => new MultipleContainer(syncedDirs)
};

switch (countainerDef.Type) {
case ContainerType.Analysis:
config.Analysis = def;
break;
case ContainerType.Coverage:
config.Coverage = def;
break;
case ContainerType.Crashes:
config.Crashes = def;
break;
case ContainerType.Inputs:
config.Inputs = def;
break;
case ContainerType.NoRepro:
config.NoRepro = def;
break;
case ContainerType.ReadonlyInputs:
config.ReadonlyInputs = def;
break;
case ContainerType.Reports:
config.Reports = def;
break;
case ContainerType.Tools:
config.Tools = def;
break;
case ContainerType.UniqueInputs:
config.UniqueInputs = def;
break;
case ContainerType.UniqueReports:
config.UniqueReports = def;
break;
case ContainerType.RegressionReports:
config.RegressionReports = def;
break;
case ContainerType.Extra:
config.Extra = def;
break;
case ContainerType.ExtraRw:
config.ExtraRw = def;
break;
default:
throw new InvalidDataException($"unknown container type: {countainerDef.Type}");
}
}
}

Expand Down
Loading

0 comments on commit c04872b

Please sign in to comment.