From 5313d797e90cb113845094404b0ce165ec02ec90 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 15 May 2018 17:37:17 -0700 Subject: [PATCH 1/5] ignore pal_config.h --- src/libpsl-native/src/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/libpsl-native/src/.gitignore diff --git a/src/libpsl-native/src/.gitignore b/src/libpsl-native/src/.gitignore new file mode 100644 index 00000000000..5c8aecc64be --- /dev/null +++ b/src/libpsl-native/src/.gitignore @@ -0,0 +1 @@ +pal_config.h From 25d19a47fc71f806d573847dac6074b4ffb70dbf Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 15 May 2018 17:37:37 -0700 Subject: [PATCH 2/5] Add comment to getstat not to use in managed code --- src/libpsl-native/src/getstat.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libpsl-native/src/getstat.cpp b/src/libpsl-native/src/getstat.cpp index dba95262a7a..757c5c3ecff 100644 --- a/src/libpsl-native/src/getstat.cpp +++ b/src/libpsl-native/src/getstat.cpp @@ -34,6 +34,10 @@ //! @retval -1 if failed //! +// DO NOT use in managed code +// use externally defined structs in managed code has proven to be buggy +// (memory corruption issues due to layout difference between platforms) +// see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 int32_t GetStat(const char* path, struct stat* buf) { assert(path); From 1c1b6648417885d2e8a192a666e17c7f25e7a94e Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 15 May 2018 17:38:27 -0700 Subject: [PATCH 3/5] update SetDate to use private struct to avoid memory corruption Addresses #6872 --- src/libpsl-native/src/setdate.cpp | 31 ++++++++++++++++++++++++++++--- src/libpsl-native/src/setdate.h | 23 ++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/libpsl-native/src/setdate.cpp b/src/libpsl-native/src/setdate.cpp index 97709ad558d..f9129c9adce 100644 --- a/src/libpsl-native/src/setdate.cpp +++ b/src/libpsl-native/src/setdate.cpp @@ -15,22 +15,47 @@ //! @brief SetDate sets the date and time on local computer. //! You must be super-user to set the time. +//! See comment in setdate.h about the use of private_tm //! //! SetDate //! //! @retval 0 successfully set date //! @retval -1 if failure occurred. //! -int32_t SetDate(struct tm* time) +int32_t SetDate(struct private_tm* time) { + errno = 0; // Select locale from environment setlocale(LC_ALL, ""); struct timeval tv; + int32_t result = GetTimeVal(*time,tv); + if(result != 0) + { + return result; + } + + return settimeofday(&tv, NULL); +} + +static int32_t GetTimeVal(struct private_tm& time, struct timeval& tv) +{ + struct tm nativeTime; + nativeTime.tm_hour = static_cast(time.Hour); + nativeTime.tm_isdst = static_cast(time.IsDst); + nativeTime.tm_mday = static_cast(time.DayOfMonth); + nativeTime.tm_min = static_cast(time.Minutes); + nativeTime.tm_mon = static_cast(time.Month); + nativeTime.tm_sec = static_cast(time.Seconds); + nativeTime.tm_wday = static_cast(time.DayOfWeek); + nativeTime.tm_yday = static_cast(time.DayInYear); + nativeTime.tm_year = static_cast(time.Year); + //nativeTime.tm_zone; + //nativeTime.tm_gmtoff; - time_t newTime = mktime(time); + time_t newTime = mktime(&nativeTime); if (newTime == -1) { return -1; @@ -39,5 +64,5 @@ int32_t SetDate(struct tm* time) tv.tv_sec = newTime; tv.tv_usec = 0; - return settimeofday(&tv, NULL); + return 0; } diff --git a/src/libpsl-native/src/setdate.h b/src/libpsl-native/src/setdate.h index 250cf515e59..443e87c378a 100644 --- a/src/libpsl-native/src/setdate.h +++ b/src/libpsl-native/src/setdate.h @@ -9,6 +9,27 @@ PAL_BEGIN_EXTERNC -int32_t SetDate(struct tm* time); +int32_t SetDate(struct private_tm* time); + +static int32_t GetTimeVal(struct private_tm& time, struct timeval& tv); PAL_END_EXTERNC + +// Using a private struct because theuse externally defined structs +// in managed code has proven to be buggy +// (memory corruption issues due to layout difference between platforms) +// see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 +#pragma pack(push, 4) // exact fit - no padding +struct private_tm +{ + int32_t Seconds; /* Seconds (0-60) */ + int32_t Minutes; /* Minutes (0-59) */ + int32_t Hour; /* Hours (0-23) */ + int32_t DayOfMonth;/* Day of the month (1-31) */ + int32_t Month; /* Month (0-11) */ + int32_t Year; /* Year - 1900 */ + int32_t DayOfWeek; /* Day of the week (0-6, Sunday = 0) */ + int32_t DayInYear; /* Day in the year (0-365, 1 Jan = 0) */ + int32_t IsDst; /* Daylight saving time */ +}; +#pragma pack(pop) //back to whatever the previous packing mode was From a2ca44e7f645a734b866ea82ee890e81d6540dd3 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 15 May 2018 17:42:58 -0700 Subject: [PATCH 4/5] Add dan as a codeowner of libpsl-native --- .github/CODEOWNERS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 441c1ed6229..02f08a797e1 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -68,6 +68,9 @@ src/System.Management.Automation/engine/remoting @dantraMSFT @mirichmo @PaulHi # Area: Side-By-Side # @mirichmo @charub +# Area: Libpsl-native +src/libpsl-native @dantraMSFT + # Areas: Build # Must be last *.config @daxian-dbw @TravisEz13 @adityapatwardhan From b88133eb1e0ef0c767fe1b9c798c1a89b80e0576 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Tue, 15 May 2018 17:56:30 -0700 Subject: [PATCH 5/5] fix CodeFactor issues --- src/libpsl-native/src/getstat.cpp | 2 +- src/libpsl-native/src/setdate.cpp | 21 +++++++++------------ src/libpsl-native/src/setdate.h | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/libpsl-native/src/getstat.cpp b/src/libpsl-native/src/getstat.cpp index 757c5c3ecff..2cc6f66ce36 100644 --- a/src/libpsl-native/src/getstat.cpp +++ b/src/libpsl-native/src/getstat.cpp @@ -35,7 +35,7 @@ //! // DO NOT use in managed code -// use externally defined structs in managed code has proven to be buggy +// use externally defined structs in managed code has proven to be buggy // (memory corruption issues due to layout difference between platforms) // see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 int32_t GetStat(const char* path, struct stat* buf) diff --git a/src/libpsl-native/src/setdate.cpp b/src/libpsl-native/src/setdate.cpp index f9129c9adce..b5d7b70dde1 100644 --- a/src/libpsl-native/src/setdate.cpp +++ b/src/libpsl-native/src/setdate.cpp @@ -24,7 +24,6 @@ //! int32_t SetDate(struct private_tm* time) { - errno = 0; // Select locale from environment @@ -42,18 +41,16 @@ int32_t SetDate(struct private_tm* time) static int32_t GetTimeVal(struct private_tm& time, struct timeval& tv) { - struct tm nativeTime; - nativeTime.tm_hour = static_cast(time.Hour); + struct tm nativeTime = 0; + nativeTime.tm_hour = static_cast(time.Hour); nativeTime.tm_isdst = static_cast(time.IsDst); - nativeTime.tm_mday = static_cast(time.DayOfMonth); - nativeTime.tm_min = static_cast(time.Minutes); - nativeTime.tm_mon = static_cast(time.Month); - nativeTime.tm_sec = static_cast(time.Seconds); - nativeTime.tm_wday = static_cast(time.DayOfWeek); - nativeTime.tm_yday = static_cast(time.DayInYear); - nativeTime.tm_year = static_cast(time.Year); - //nativeTime.tm_zone; - //nativeTime.tm_gmtoff; + nativeTime.tm_mday = static_cast(time.DayOfMonth); + nativeTime.tm_min = static_cast(time.Minutes); + nativeTime.tm_mon = static_cast(time.Month); + nativeTime.tm_sec = static_cast(time.Seconds); + nativeTime.tm_wday = static_cast(time.DayOfWeek); + nativeTime.tm_yday = static_cast(time.DayInYear); + nativeTime.tm_year = static_cast(time.Year); time_t newTime = mktime(&nativeTime); if (newTime == -1) diff --git a/src/libpsl-native/src/setdate.h b/src/libpsl-native/src/setdate.h index 443e87c378a..4d69f1e0a7c 100644 --- a/src/libpsl-native/src/setdate.h +++ b/src/libpsl-native/src/setdate.h @@ -16,7 +16,7 @@ static int32_t GetTimeVal(struct private_tm& time, struct timeval& tv); PAL_END_EXTERNC // Using a private struct because theuse externally defined structs -// in managed code has proven to be buggy +// in managed code has proven to be buggy // (memory corruption issues due to layout difference between platforms) // see https://github.com/dotnet/corefx/issues/29700#issuecomment-389313075 #pragma pack(push, 4) // exact fit - no padding @@ -32,4 +32,4 @@ struct private_tm int32_t DayInYear; /* Day in the year (0-365, 1 Jan = 0) */ int32_t IsDst; /* Daylight saving time */ }; -#pragma pack(pop) //back to whatever the previous packing mode was +#pragma pack(pop) //back to whatever the previous packing mode was