Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / platform_qual_test / nacl_cpuwhitelist.c
blob7e6d6ca3962cc8d794e839d92c3a41fd8474393a
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * nacl_cpuwhitelist.c
35 #include "native_client/include/portability.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include <assert.h>
39 #include <stdlib.h>
40 #include "native_client/ncv/nacl_cpuid.h"
41 #include "native_client/platform_qual_test/nacl_cpuwhitelist.h"
43 #if !defined(ARRAYSIZE)
44 #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
45 #endif
47 static int idcmp(const void *s1, const void *s2) {
48 return strncmp((char *)s1, *(char **)s2, kCPUIDStringLength);
53 static const char* const kNaclCpuBlacklist[] = {
54 /* 1 2 */
55 /* 12345678901234567890 + '\0' */
56 " FakeEntry0000000000",
57 " FakeEntry0000000001",
58 "zFakeEntry0000000000",
61 static const char* const kNaclCpuWhitelist[] = {
62 /* 1 2 */
63 /* 12345678901234567890 + '\0' */
64 " FakeEntry0000000000",
65 " FakeEntry0000000001",
66 " FakeEntry0000000002",
67 " FakeEntry0000000003",
68 " FakeEntry0000000004",
69 " FakeEntry0000000005",
70 "GenuineIntel00000f43",
71 "zFakeEntry0000000000",
76 static bool VerifyCpuList(const char* const cpulist[], int n) {
77 int i;
78 /* check lengths */
79 for (i = 0; i < n; i++) {
80 if (strlen(cpulist[i]) != kCPUIDStringLength - 1) {
81 return 0;
85 /* check order */
86 for (i = 1; i < n; i++) {
87 if (strncmp(cpulist[i-1], cpulist[i], kCPUIDStringLength) >= 0) {
88 return 0;
92 return 1;
96 bool NaCl_VerifyBlacklist() {
97 return VerifyCpuList(kNaclCpuBlacklist, ARRAYSIZE(kNaclCpuBlacklist));
101 bool NaCl_VerifyWhitelist() {
102 return VerifyCpuList(kNaclCpuWhitelist, ARRAYSIZE(kNaclCpuWhitelist));
106 static bool IsCpuInList(const char *myid, const char* const cpulist[], int n) {
107 return (NULL != bsearch(myid, cpulist, n, sizeof(char*), idcmp));
111 /* for testing */
112 bool NaCl_CPUIsWhitelisted(const char *myid) {
113 return IsCpuInList(myid, kNaclCpuWhitelist, ARRAYSIZE(kNaclCpuWhitelist));
116 /* for testing */
117 bool NaCl_CPUIsBlacklisted(const char *myid) {
118 return IsCpuInList(myid, kNaclCpuBlacklist, ARRAYSIZE(kNaclCpuBlacklist));
122 bool NaCl_ThisCPUIsWhitelisted() {
123 const char* myid = GetCPUIDString();
124 return IsCpuInList(myid, kNaclCpuWhitelist, ARRAYSIZE(kNaclCpuWhitelist));
127 bool NaCl_ThisCPUIsBlacklisted() {
128 const char* myid = GetCPUIDString();
129 return IsCpuInList(myid, kNaclCpuBlacklist, ARRAYSIZE(kNaclCpuBlacklist));