mirror of
https://github.com/google/cpu_features.git
synced 2025-04-27 07:02:30 +02:00
Avoid leaking internal headers for ppc (#164)
This commit is contained in:
parent
001faefdc3
commit
b3ef4ef49d
@ -17,7 +17,6 @@
|
||||
|
||||
#include "cpu_features_cache_info.h"
|
||||
#include "cpu_features_macros.h"
|
||||
#include "internal/hwcaps.h"
|
||||
|
||||
CPU_FEATURES_START_CPP_NAMESPACE
|
||||
|
||||
@ -74,12 +73,17 @@ typedef struct {
|
||||
// This function is guaranteed to be malloc, memset and memcpy free.
|
||||
PPCInfo GetPPCInfo(void);
|
||||
|
||||
typedef struct {
|
||||
char platform[64]; // 0 terminated string
|
||||
char base_platform[64]; // 0 terminated string
|
||||
} PPCPlatformTypeStrings;
|
||||
|
||||
typedef struct {
|
||||
char platform[64]; // 0 terminated string
|
||||
char model[64]; // 0 terminated string
|
||||
char machine[64]; // 0 terminated string
|
||||
char cpu[64]; // 0 terminated string
|
||||
PlatformType type;
|
||||
PPCPlatformTypeStrings type;
|
||||
} PPCPlatformStrings;
|
||||
|
||||
PPCPlatformStrings GetPPCPlatformStrings(void);
|
||||
|
@ -171,16 +171,19 @@ typedef struct {
|
||||
unsigned long hwcaps2;
|
||||
} HardwareCapabilities;
|
||||
|
||||
// Retrieves values from auxiliary vector for types AT_HWCAP and AT_HWCAP2.
|
||||
// First tries to call getauxval(), if not available falls back to reading
|
||||
// "/proc/self/auxv".
|
||||
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);
|
||||
|
||||
// Checks whether value for AT_HWCAP (or AT_HWCAP2) match hwcaps_mask.
|
||||
bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
|
||||
const HardwareCapabilities hwcaps);
|
||||
|
||||
typedef struct {
|
||||
char platform[64]; // 0 terminated string
|
||||
char base_platform[64]; // 0 terminated string
|
||||
} PlatformType;
|
||||
|
||||
PlatformType CpuFeatures_GetPlatformType(void);
|
||||
// Get pointer for the AT_PLATFORM type.
|
||||
const char* CpuFeatures_GetPlatformPointer(void);
|
||||
// Get pointer for the AT_BASE_PLATFORM type.
|
||||
const char* CpuFeatures_GetBasePlatformPointer(void);
|
||||
|
||||
CPU_FEATURES_END_CPP_NAMESPACE
|
||||
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
#include "internal/bit_utils.h"
|
||||
#include "internal/filesystem.h"
|
||||
#include "internal/hwcaps.h"
|
||||
#include "internal/stack_line_reader.h"
|
||||
#include "internal/string_view.h"
|
||||
|
||||
@ -133,9 +134,19 @@ static const PPCPlatformStrings kEmptyPPCPlatformStrings;
|
||||
|
||||
PPCPlatformStrings GetPPCPlatformStrings(void) {
|
||||
PPCPlatformStrings strings = kEmptyPPCPlatformStrings;
|
||||
const char* platform = CpuFeatures_GetPlatformPointer();
|
||||
const char* base_platform = CpuFeatures_GetBasePlatformPointer();
|
||||
|
||||
FillProcCpuInfoData(&strings);
|
||||
strings.type = CpuFeatures_GetPlatformType();
|
||||
|
||||
if (platform != NULL)
|
||||
CpuFeatures_StringView_CopyString(str(platform), strings.type.platform,
|
||||
sizeof(strings.type.platform));
|
||||
if (base_platform != NULL)
|
||||
CpuFeatures_StringView_CopyString(str(base_platform),
|
||||
strings.type.base_platform,
|
||||
sizeof(strings.type.base_platform));
|
||||
|
||||
return strings;
|
||||
}
|
||||
|
||||
|
21
src/hwcaps.c
21
src/hwcaps.c
@ -35,7 +35,8 @@ bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
|
||||
#ifdef CPU_FEATURES_TEST
|
||||
// In test mode, hwcaps_for_testing will define the following functions.
|
||||
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);
|
||||
PlatformType CpuFeatures_GetPlatformType(void);
|
||||
const char* CpuFeatures_GetPlatformPointer(void);
|
||||
const char* CpuFeatures_GetBasePlatformPointer(void);
|
||||
#else
|
||||
|
||||
// Debug facilities
|
||||
@ -163,20 +164,12 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
|
||||
return capabilities;
|
||||
}
|
||||
|
||||
PlatformType kEmptyPlatformType;
|
||||
const char *CpuFeatures_GetPlatformPointer(void) {
|
||||
return (const char *)GetHardwareCapabilitiesFor(AT_PLATFORM);
|
||||
}
|
||||
|
||||
PlatformType CpuFeatures_GetPlatformType(void) {
|
||||
PlatformType type = kEmptyPlatformType;
|
||||
char *platform = (char *)GetHardwareCapabilitiesFor(AT_PLATFORM);
|
||||
char *base_platform = (char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
|
||||
|
||||
if (platform != NULL)
|
||||
CpuFeatures_StringView_CopyString(str(platform), type.platform,
|
||||
sizeof(type.platform));
|
||||
if (base_platform != NULL)
|
||||
CpuFeatures_StringView_CopyString(str(base_platform), type.base_platform,
|
||||
sizeof(type.base_platform));
|
||||
return type;
|
||||
const char *CpuFeatures_GetBasePlatformPointer(void) {
|
||||
return (const char *)GetHardwareCapabilitiesFor(AT_BASE_PLATFORM);
|
||||
}
|
||||
|
||||
#endif // CPU_FEATURES_TEST
|
||||
|
@ -24,6 +24,7 @@ namespace {
|
||||
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }
|
||||
|
||||
TEST(CpuinfoAarch64Test, FromHardwareCap) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(AARCH64_HWCAP_FP | AARCH64_HWCAP_AES, 0);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
const auto info = GetAarch64Info();
|
||||
@ -62,6 +63,7 @@ TEST(CpuinfoAarch64Test, FromHardwareCap) {
|
||||
}
|
||||
|
||||
TEST(CpuinfoAarch64Test, FromHardwareCap2) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(AARCH64_HWCAP_FP,
|
||||
AARCH64_HWCAP2_SVE2 | AARCH64_HWCAP2_BTI);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
@ -90,7 +92,7 @@ TEST(CpuinfoAarch64Test, FromHardwareCap2) {
|
||||
}
|
||||
|
||||
TEST(CpuinfoAarch64Test, ARMCortexA53) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(Processor : AArch64 Processor rev 3 (aarch64)
|
||||
|
@ -21,9 +21,8 @@
|
||||
namespace cpu_features {
|
||||
namespace {
|
||||
|
||||
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }
|
||||
|
||||
TEST(CpuinfoArmTest, FromHardwareCap) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(ARM_HWCAP_NEON, ARM_HWCAP2_AES | ARM_HWCAP2_CRC32);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
const auto info = GetArmInfo();
|
||||
@ -52,7 +51,7 @@ TEST(CpuinfoArmTest, FromHardwareCap) {
|
||||
}
|
||||
|
||||
TEST(CpuinfoArmTest, ODroidFromCpuInfo) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
|
||||
model name : ARMv7 Processor rev 3 (v71)
|
||||
@ -101,7 +100,7 @@ CPU revision : 3)");
|
||||
|
||||
// Linux test-case
|
||||
TEST(CpuinfoArmTest, RaspberryPiZeroFromCpuInfo) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
|
||||
model name : ARMv6-compatible processor rev 7 (v6l)
|
||||
@ -153,7 +152,7 @@ Serial : 000000006cd946f3)");
|
||||
}
|
||||
|
||||
TEST(CpuinfoArmTest, MarvellArmadaFromCpuInfo) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo", R"(processor : 0
|
||||
model name : ARMv7 Processor rev 1 (v7l)
|
||||
@ -217,7 +216,7 @@ Serial : 0000000000000000)");
|
||||
// Android test-case
|
||||
// http://code.google.com/p/android/issues/detail?id=10812
|
||||
TEST(CpuinfoArmTest, InvalidArmv7) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(Processor : ARMv6-compatible processor rev 6 (v6l)
|
||||
@ -267,6 +266,7 @@ Serial : 33323613546d00ec )");
|
||||
// Android test-case
|
||||
// https://crbug.com/341598.
|
||||
TEST(CpuinfoArmTest, InvalidNeon) {
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(Processor: ARMv7 Processory rev 0 (v71)
|
||||
@ -294,7 +294,7 @@ Serial: 00001e030000354e)");
|
||||
// The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report IDIV
|
||||
// support.
|
||||
TEST(CpuinfoArmTest, Nexus4_0x510006f2) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(CPU implementer : 0x51
|
||||
@ -312,7 +312,7 @@ CPU revision : 2)");
|
||||
// The Nexus 4 (Qualcomm Krait) kernel configuration forgets to report IDIV
|
||||
// support.
|
||||
TEST(CpuinfoArmTest, Nexus4_0x510006f3) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(CPU implementer : 0x51
|
||||
@ -331,7 +331,7 @@ CPU revision : 3)");
|
||||
// 32-bit ARM IDIV instruction. Technically, this is a feature of the virtual
|
||||
// CPU implemented by the emulator.
|
||||
TEST(CpuinfoArmTest, EmulatorSpecificIdiv) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(Processor : ARMv7 Processor rev 0 (v7l)
|
||||
|
@ -24,9 +24,8 @@ namespace cpu_features {
|
||||
|
||||
namespace {
|
||||
|
||||
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }
|
||||
|
||||
TEST(CpuinfoMipsTest, FromHardwareCapBoth) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(MIPS_HWCAP_MSA | MIPS_HWCAP_R6, 0);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
const auto info = GetMipsInfo();
|
||||
@ -36,6 +35,7 @@ TEST(CpuinfoMipsTest, FromHardwareCapBoth) {
|
||||
}
|
||||
|
||||
TEST(CpuinfoMipsTest, FromHardwareCapOnlyOne) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(MIPS_HWCAP_MSA, 0);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
const auto info = GetMipsInfo();
|
||||
@ -44,7 +44,7 @@ TEST(CpuinfoMipsTest, FromHardwareCapOnlyOne) {
|
||||
}
|
||||
|
||||
TEST(CpuinfoMipsTest, Ci40) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo", R"(system type : IMG Pistachio SoC (B0)
|
||||
machine : IMG Marduk – Ci40 with cc2520
|
||||
@ -72,7 +72,7 @@ VPE : 0
|
||||
}
|
||||
|
||||
TEST(CpuinfoMipsTest, AR7161) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(system type : Atheros AR7161 rev 2
|
||||
@ -98,7 +98,7 @@ VCEI exceptions : not available
|
||||
}
|
||||
|
||||
TEST(CpuinfoMipsTest, Goldfish) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo", R"(system type : MIPS-Goldfish
|
||||
Hardware : goldfish
|
||||
|
@ -22,9 +22,8 @@
|
||||
namespace cpu_features {
|
||||
namespace {
|
||||
|
||||
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }
|
||||
|
||||
TEST(CpustringsPPCTest, FromHardwareCap) {
|
||||
ResetHwcaps();
|
||||
SetHardwareCapabilities(PPC_FEATURE_HAS_FPU | PPC_FEATURE_HAS_VSX,
|
||||
PPC_FEATURE2_ARCH_3_00);
|
||||
GetEmptyFilesystem(); // disabling /proc/cpuinfo
|
||||
@ -40,7 +39,7 @@ TEST(CpustringsPPCTest, FromHardwareCap) {
|
||||
}
|
||||
|
||||
TEST(CpustringsPPCTest, Blade) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(processor : 14
|
||||
@ -57,7 +56,8 @@ timebase : 512000000
|
||||
platform : pSeries
|
||||
model : IBM,8406-70Y
|
||||
machine : CHRP IBM,8406-70Y)");
|
||||
SetPlatformTypes("power7", "power8");
|
||||
SetPlatformPointer("power7");
|
||||
SetBasePlatformPointer("power8");
|
||||
const auto strings = GetPPCPlatformStrings();
|
||||
ASSERT_STREQ(strings.platform, "pSeries");
|
||||
ASSERT_STREQ(strings.model, "IBM,8406-70Y");
|
||||
@ -68,7 +68,7 @@ machine : CHRP IBM,8406-70Y)");
|
||||
}
|
||||
|
||||
TEST(CpustringsPPCTest, Firestone) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(processor : 126
|
||||
@ -94,7 +94,7 @@ firmware : OPAL v3)");
|
||||
}
|
||||
|
||||
TEST(CpustringsPPCTest, w8) {
|
||||
DisableHardwareCapabilities();
|
||||
ResetHwcaps();
|
||||
auto& fs = GetEmptyFilesystem();
|
||||
fs.CreateFile("/proc/cpuinfo",
|
||||
R"(processor : 143
|
||||
|
@ -22,25 +22,31 @@ namespace cpu_features {
|
||||
|
||||
namespace {
|
||||
static auto* const g_hardware_capabilities = new HardwareCapabilities();
|
||||
static auto* const g_platform_types = new PlatformType();
|
||||
static const char* g_platform_pointer = nullptr;
|
||||
static const char* g_base_platform_pointer = nullptr;
|
||||
} // namespace
|
||||
|
||||
void SetHardwareCapabilities(uint32_t hwcaps, uint32_t hwcaps2) {
|
||||
g_hardware_capabilities->hwcaps = hwcaps;
|
||||
g_hardware_capabilities->hwcaps2 = hwcaps2;
|
||||
}
|
||||
void SetPlatformPointer(const char* string) { g_platform_pointer = string; }
|
||||
void SetBasePlatformPointer(const char* string) {
|
||||
g_base_platform_pointer = string;
|
||||
}
|
||||
|
||||
void ResetHwcaps() {
|
||||
SetHardwareCapabilities(0, 0);
|
||||
SetPlatformPointer(nullptr);
|
||||
SetBasePlatformPointer(nullptr);
|
||||
}
|
||||
|
||||
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
|
||||
return *g_hardware_capabilities;
|
||||
}
|
||||
|
||||
void SetPlatformTypes(const char* platform, const char* base_platform) {
|
||||
CpuFeatures_StringView_CopyString(str(platform), g_platform_types->platform,
|
||||
sizeof(g_platform_types->platform));
|
||||
CpuFeatures_StringView_CopyString(str(base_platform),
|
||||
g_platform_types->base_platform,
|
||||
sizeof(g_platform_types->base_platform));
|
||||
const char* CpuFeatures_GetPlatformPointer(void) { return g_platform_pointer; }
|
||||
const char* CpuFeatures_GetBasePlatformPointer(void) {
|
||||
return g_base_platform_pointer;
|
||||
}
|
||||
|
||||
PlatformType CpuFeatures_GetPlatformType(void) { return *g_platform_types; }
|
||||
} // namespace cpu_features
|
||||
|
@ -20,7 +20,11 @@
|
||||
namespace cpu_features {
|
||||
|
||||
void SetHardwareCapabilities(uint32_t hwcaps, uint32_t hwcaps2);
|
||||
void SetPlatformTypes(const char *platform, const char *base_platform);
|
||||
void SetPlatformPointer(const char* string);
|
||||
void SetBasePlatformPointer(const char* string);
|
||||
|
||||
// To be called before each test.
|
||||
void ResetHwcaps();
|
||||
|
||||
} // namespace cpu_features
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user