Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…Net-msbuild-Trusted into 17.4-security-update
  • Loading branch information
Forgind committed Oct 11, 2022
2 parents e1930c2 + 6521b15 commit c478a85
Show file tree
Hide file tree
Showing 28 changed files with 174 additions and 83 deletions.
16 changes: 7 additions & 9 deletions src/Build.OM.UnitTests/Definition/Project_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,7 @@ public void TransformsUseCorrectDirectory_Basic()
project.ReevaluateIfNecessary();

project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(
NativeMethodsShared.IsWindows
? Path.Combine(Path.GetTempPath(), @"obj\i386\foo.dll")
: Path.Combine(Path.GetTempPath(), @"obj/i386/foo.dll"));
Path.Combine(FileUtilities.TempFileDirectory, "obj", "i386", "foo.dll"));
}
finally
{
Expand Down Expand Up @@ -721,8 +719,8 @@ public void TransformsUseCorrectDirectory_DirectoryTransform()
Project project = new Project(xml);
ProjectInstance projectInstance = new ProjectInstance(xml);

project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath(), "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath(), "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory, "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory, "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
}
finally
{
Expand Down Expand Up @@ -756,8 +754,8 @@ public void TransformsUseCorrectDirectory_DirectoryItemFunction()
Project project = new Project(xml);
ProjectInstance projectInstance = new ProjectInstance(xml);

project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath(), "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath(), "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory, "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory, "obj", "i386").Substring(RootPrefixLength) + Path.DirectorySeparatorChar);
}
finally
{
Expand Down Expand Up @@ -794,8 +792,8 @@ public void TransformsUseCorrectDirectory_DirectoryNameItemFunction()
ProjectInstance projectInstance = new ProjectInstance(xml);

// Should be the full path to the directory
project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath() /* remove c:\ */, "obj" + Path.DirectorySeparatorChar + "i386"));
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(Path.GetTempPath() /* remove c:\ */, "obj" + Path.DirectorySeparatorChar + "i386"));
project.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory /* remove c:\ */, "obj" + Path.DirectorySeparatorChar + "i386"));
projectInstance.GetItems("BuiltProjectOutputGroupKeyOutput").First().EvaluatedInclude.ShouldBe(Path.Combine(FileUtilities.TempFileDirectory /* remove c:\ */, "obj" + Path.DirectorySeparatorChar + "i386"));
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ public void TestCache2()
Environment.SetEnvironmentVariable("TEMP", problematicTmpPath);

FileUtilities.ClearCacheDirectoryPath();
FileUtilities.ClearTempFileDirectory();
string cacheFilePath = configuration.GetCacheFile();
Assert.StartsWith(problematicTmpPath, cacheFilePath);
}
Expand All @@ -484,6 +485,7 @@ public void TestCache2()
Environment.SetEnvironmentVariable("TMP", originalTmp);
Environment.SetEnvironmentVariable("TEMP", originalTemp);
FileUtilities.ClearCacheDirectoryPath();
FileUtilities.ClearTempFileDirectory();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/DebugUtils_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void DumpExceptionToFileShouldWriteInTempPathByDefault()
try
{
ExceptionHandling.DumpExceptionToFile(new Exception("hello world"));
exceptionFiles = Directory.GetFiles(Path.GetTempPath(), "MSBuild_*failure.txt");
exceptionFiles = Directory.GetFiles(FileUtilities.TempFileDirectory, "MSBuild_*failure.txt");
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/Build.UnitTests/BackEnd/TargetUpToDateChecker_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ public void NewSymlinkNewDestinationIsNotUpToDate()
_testOutputHelper.WriteLine($"Created input file {inputTarget}");
File.SetLastWriteTime(inputTarget, targetWriteTime);

inputSymlink = FileUtilities.GetTemporaryFile(null, ".linkin", createFile: false);
inputSymlink = FileUtilities.GetTemporaryFile(null, null, ".linkin", createFile: false);

if (!CreateSymbolicLink(inputSymlink, inputTarget, 0))
{
Expand Down
20 changes: 10 additions & 10 deletions src/Build.UnitTests/Construction/SolutionFile_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void ParseFirstProjectLine_InvalidProject()
[Fact]
public void ParseEtpProject()
{
string proj1Path = Path.Combine(Path.GetTempPath(), "someproj.etp");
string proj1Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj.etp");
try
{
// Create the first .etp project file
Expand Down Expand Up @@ -192,8 +192,8 @@ public void ParseEtpProject()
[Fact]
public void CanBeMSBuildFile()
{
string proj1Path = Path.Combine(Path.GetTempPath(), "someproj.etp");
string proj2Path = Path.Combine(Path.GetTempPath(), "someproja.proj");
string proj1Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj.etp");
string proj2Path = Path.Combine(FileUtilities.TempFileDirectory, "someproja.proj");
try
{
// Create the first .etp project file
Expand Down Expand Up @@ -317,8 +317,8 @@ public void CanBeMSBuildFileRejectsMSBuildLikeFiles()
[Fact]
public void ParseNestedEtpProjectSingleLevel()
{
string proj1Path = Path.Combine(Path.GetTempPath(), "someproj.etp");
string proj2Path = Path.Combine(Path.GetTempPath(), "someproj2.etp");
string proj1Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj.etp");
string proj2Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj2.etp");
try
{
// Create the first .etp project file
Expand Down Expand Up @@ -513,9 +513,9 @@ public void TestVSAndSolutionVersionParsing()
[Trait("Category", "netcore-linux-failing")]
public void ParseNestedEtpProjectMultipleLevel()
{
string proj1Path = Path.Combine(Path.GetTempPath(), "someproj.etp");
string proj2Path = Path.Combine(Path.GetTempPath(), "someproj2.etp");
string proj3Path = Path.Combine(Path.GetTempPath(), "ETPProjUpgradeTest", "someproj3.etp");
string proj1Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj.etp");
string proj2Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj2.etp");
string proj3Path = Path.Combine(FileUtilities.TempFileDirectory, "ETPProjUpgradeTest", "someproj3.etp");
try
{
// Create the first .etp project file
Expand Down Expand Up @@ -567,7 +567,7 @@ public void ParseNestedEtpProjectMultipleLevel()
</GENERAL>
</EFPROJECT>";
// Create the directory for the third project
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "ETPProjUpgradeTest"));
Directory.CreateDirectory(Path.Combine(FileUtilities.TempFileDirectory, "ETPProjUpgradeTest"));
File.WriteAllText(proj3Path, etpProjContent);

// Create the SolutionFile object
Expand Down Expand Up @@ -602,7 +602,7 @@ public void ParseNestedEtpProjectMultipleLevel()
[Fact]
public void MalformedEtpProjFile()
{
string proj1Path = Path.Combine(Path.GetTempPath(), "someproj.etp");
string proj1Path = Path.Combine(FileUtilities.TempFileDirectory, "someproj.etp");
try
{
// Create the .etp project file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public void SolutionConfigurationWithDependencies()
<ProjectConfiguration Project=`{{786E302A-96CE-43DC-B640-D6B6CC9BF6C0}}` AbsolutePath=`##temp##{Path.Combine("Project1", "A.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU</ProjectConfiguration>
<ProjectConfiguration Project=`{{881C1674-4ECA-451D-85B6-D7C59B7F16FA}}` AbsolutePath=`##temp##{Path.Combine("Project2", "B.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU<ProjectDependency Project=`{{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}}` /></ProjectConfiguration>
<ProjectConfiguration Project=`{{4A727FF8-65F2-401E-95AD-7C8BBFBE3167}}` AbsolutePath=`##temp##{Path.Combine("Project3", "C.csproj")}` BuildProjectInSolution=`True`>Debug|AnyCPU</ProjectConfiguration>
</SolutionConfiguration>".Replace("`", "\"").Replace("##temp##", Path.GetTempPath());
</SolutionConfiguration>".Replace("`", "\"").Replace("##temp##", FileUtilities.TempFileDirectory);

Helpers.VerifyAssertLineByLine(expected, solutionConfigurationContents);
}
Expand Down Expand Up @@ -1090,14 +1090,14 @@ public void TestAddPropertyGroupForSolutionConfiguration()
msbuildProject.ReevaluateIfNecessary();

string solutionConfigurationContents = msbuildProject.GetPropertyValue("CurrentSolutionConfigurationContents");
string tempProjectPath = Path.Combine(Path.GetTempPath(), "ClassLibrary1", "ClassLibrary1.csproj");
string tempProjectPath = Path.Combine(FileUtilities.TempFileDirectory, "ClassLibrary1", "ClassLibrary1.csproj");

Assert.Contains("{6185CC21-BE89-448A-B3C0-D1C27112E595}", solutionConfigurationContents);
tempProjectPath = Path.GetFullPath(tempProjectPath);
Assert.True(solutionConfigurationContents.IndexOf(tempProjectPath, StringComparison.OrdinalIgnoreCase) > 0);
Assert.Contains("CSConfig1|AnyCPU", solutionConfigurationContents);

tempProjectPath = Path.Combine(Path.GetTempPath(), "MainApp", "MainApp.vcxproj");
tempProjectPath = Path.Combine(FileUtilities.TempFileDirectory, "MainApp", "MainApp.vcxproj");
tempProjectPath = Path.GetFullPath(tempProjectPath);
Assert.Contains("{A6F99D27-47B9-4EA4-BFC9-25157CBDC281}", solutionConfigurationContents);
Assert.True(solutionConfigurationContents.IndexOf(tempProjectPath, StringComparison.OrdinalIgnoreCase) > 0);
Expand Down
4 changes: 2 additions & 2 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3052,7 +3052,7 @@ public void PropertyFunctionStaticMethodEnumArgument()
[Fact]
public void PropertyFunctionStaticMethodDirectoryNameOfFileAbove()
{
string tempPath = Path.GetTempPath();
string tempPath = FileUtilities.TempFileDirectory;
string tempFile = Path.GetFileName(FileUtilities.GetTemporaryFile());

try
Expand Down Expand Up @@ -3090,7 +3090,7 @@ public void PropertyFunctionStaticMethodGetPathOfFileAbove()
//
MockElementLocation mockElementLocation = new MockElementLocation(Path.Combine(ObjectModelHelpers.TempProjectDir, "one", "two", "three", "four", "five", Path.GetRandomFileName()));

string fileToFind = FileUtilities.GetTemporaryFile(ObjectModelHelpers.TempProjectDir, ".tmp");
string fileToFind = FileUtilities.GetTemporaryFile(ObjectModelHelpers.TempProjectDir, null, ".tmp");

try
{
Expand Down
8 changes: 8 additions & 0 deletions src/Build/BackEnd/BuildManager/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ public DeferredBuildMessage(string text, MessageImportance importance)
/// <exception cref="InvalidOperationException">Thrown if a build is already in progress.</exception>
public void BeginBuild(BuildParameters parameters, IEnumerable<DeferredBuildMessage> deferredBuildMessages)
{
// TEMP can be modified from the environment. Most of Traits is lasts for the duration of the process (with a manual reset for tests)
// and environment variables we use as properties are stored in a dictionary at the beginning of the build, so they also cannot be
// changed during a build. Some of our older stuff uses live environment variable checks. The TEMP directory previously used a live
// environment variable check, but it now uses a cached value. Nevertheless, we should support changing it between builds, so reset
// it here in case the user is using Visual Studio or the MSBuild server, as those each last for multiple builds without changing
// BuildManager.
FileUtilities.ClearTempFileDirectory();

// deferredBuildMessages cannot be an optional parameter on a single BeginBuild method because it would break binary compatibility.
_deferredBuildMessages = deferredBuildMessages;
BeginBuild(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal BuildRequestEngine()

if (String.IsNullOrEmpty(_debugDumpPath))
{
_debugDumpPath = Path.GetTempPath();
_debugDumpPath = FileUtilities.TempFileDirectory;
}

_status = BuildRequestEngineStatus.Uninitialized;
Expand Down
2 changes: 1 addition & 1 deletion src/Build/BackEnd/Components/Scheduler/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public Scheduler()

if (String.IsNullOrEmpty(_debugDumpPath))
{
_debugDumpPath = Path.GetTempPath();
_debugDumpPath = FileUtilities.TempFileDirectory;
}

Reset();
Expand Down
12 changes: 10 additions & 2 deletions src/Framework/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ internal static bool IsUnixLike
/// <summary>
/// Gets a flag indicating if we are running under Linux
/// </summary>
[SupportedOSPlatformGuard("linux")]
internal static bool IsLinux
{
#if CLR2COMPATIBILITY
Expand Down Expand Up @@ -1470,9 +1471,16 @@ internal static void VerifyThrowWin32Result(int result)
}
}

#endregion
#endregion

#region PInvoke
[SupportedOSPlatform("linux")]
[DllImport("libc", SetLastError = true)]
internal static extern int chmod(string pathname, int mode);

#region PInvoke
[SupportedOSPlatform("linux")]
[DllImport("libc", SetLastError = true)]
internal static extern int mkdir(string path, int mode);

/// <summary>
/// Gets the current OEM code page which is used by console apps
Expand Down
2 changes: 1 addition & 1 deletion src/MSBuild/OutOfProcTaskHostNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ private NodeEngineShutdownReason HandleShutdown()

if (_debugCommunications)
{
using (StreamWriter writer = File.CreateText(String.Format(CultureInfo.CurrentCulture, Path.Combine(Path.GetTempPath(), @"MSBuild_NodeShutdown_{0}.txt"), Process.GetCurrentProcess().Id)))
using (StreamWriter writer = File.CreateText(String.Format(CultureInfo.CurrentCulture, Path.Combine(FileUtilities.TempFileDirectory, @"MSBuild_NodeShutdown_{0}.txt"), Process.GetCurrentProcess().Id)))
{
writer.WriteLine("Node shutting down with reason {0}.", _shutdownReason);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/CommunicationsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ internal static void Trace(int nodeId, string format, params object[] args)

if (String.IsNullOrEmpty(s_debugDumpPath))
{
s_debugDumpPath = Path.GetTempPath();
s_debugDumpPath = FileUtilities.TempFileDirectory;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Debugging/DebugUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static DebugUtils()
}
else
{
debugDirectory = Path.Combine(Path.GetTempPath(), "MSBuild_Logs");
debugDirectory = Path.Combine(FileUtilities.TempFileDirectory, "MSBuild_Logs");
}

// Out of proc nodes do not know the startup directory so set the environment variable for them.
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static string GetDebugDumpPath()

return !string.IsNullOrEmpty(debugPath)
? debugPath
: Path.GetTempPath();
: FileUtilities.TempFileDirectory;
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Shared/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ internal static partial class FileUtilities
/// </summary>
internal static string cacheDirectory = null;

#if CLR2COMPATIBILITY
internal static string TempFileDirectory => Path.GetTempPath();
#endif

/// <summary>
/// FOR UNIT TESTS ONLY
/// Clear out the static variable used for the cache directory so that tests that
Expand Down Expand Up @@ -122,7 +126,7 @@ internal static string GetCacheDirectory()
{
if (cacheDirectory == null)
{
cacheDirectory = Path.Combine(Path.GetTempPath(), String.Format(CultureInfo.CurrentUICulture, "MSBuild{0}-{1}", Process.GetCurrentProcess().Id, AppDomain.CurrentDomain.Id));
cacheDirectory = Path.Combine(TempFileDirectory, String.Format(CultureInfo.CurrentUICulture, "MSBuild{0}-{1}", Process.GetCurrentProcess().Id, AppDomain.CurrentDomain.Id));
}

return cacheDirectory;
Expand Down
6 changes: 6 additions & 0 deletions src/Shared/NamedPipeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ internal static string GetPlatformSpecificPipeName(string pipeName)
// can be quite long, leaving very little room for the actual pipe name. Fortunately,
// '/tmp' is mandated by POSIX to always be a valid temp directory, so we can use that
// instead.
#if !CLR2COMPATIBILITY
return Path.Combine("/tmp", pipeName);
#else
// We should never get here. This would be a net35 task host running on unix.
ErrorUtilities.ThrowInternalError("Task host used on unix in retrieving the pipe name.");
return string.Empty;
#endif
}
else
{
Expand Down
12 changes: 10 additions & 2 deletions src/Shared/NodeEndpointOutOfProcBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ internal void InternalConstruct(string pipeName = null)
PipeDirection.InOut,
1, // Only allow one connection at a time.
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous | PipeOptions.WriteThrough,
PipeOptions.Asynchronous | PipeOptions.WriteThrough
#if FEATURE_PIPEOPTIONS_CURRENTUSERONLY
| PipeOptions.CurrentUserOnly
#endif
,
PipeBufferSize, // Default input buffer
PipeBufferSize, // Default output buffer
security,
Expand All @@ -248,7 +252,11 @@ internal void InternalConstruct(string pipeName = null)
PipeDirection.InOut,
1, // Only allow one connection at a time.
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous | PipeOptions.WriteThrough,
PipeOptions.Asynchronous | PipeOptions.WriteThrough
#if FEATURE_PIPEOPTIONS_CURRENTUSERONLY
| PipeOptions.CurrentUserOnly
#endif
,
PipeBufferSize, // Default input buffer
PipeBufferSize // Default output buffer
);
Expand Down
Loading

0 comments on commit c478a85

Please sign in to comment.