Parcourir la source

Introduce IS_POWER_OF_2 macro

Michał Kowalczyk il y a 4 ans
Parent
commit
b699fd7afd

+ 1 - 2
LibOS/shim/src/elf/shim_rtld.c

@@ -300,8 +300,7 @@ void setup_elf_hash(struct link_map* map) {
         Elf32_Word symbias        = *hash32++;
         Elf32_Word bitmask_nwords = *hash32++;
 
-        /* Must be a power of two.  */
-        assert((bitmask_nwords & (bitmask_nwords - 1)) == 0);
+        assert(IS_POWER_OF_2(bitmask_nwords));
         map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
         map->l_gnu_shift           = *hash32++;
 

+ 2 - 0
Pal/lib/api.h

@@ -68,6 +68,8 @@ typedef ptrdiff_t ssize_t;
 #define SATURATED_P_SUB(ptr_a, b, limit) \
    ((__typeof__(ptr_a))SATURATED_SUB((uintptr_t)(ptr_a), (uintptr_t)(b), (uintptr_t)(limit)))
 
+#define IS_POWER_OF_2(x) (((x) & ((x) - 1)) == 0)
+
 #define ALIGN_DOWN_PTR(ptr, size) \
     ((__typeof__(ptr)) (((uintptr_t)(ptr)) & -(size)))
 #define ALIGN_UP_PTR(ptr, size) \

+ 1 - 2
Pal/src/db_rtld.c

@@ -99,8 +99,7 @@ void setup_elf_hash (struct link_map *map)
         Elf32_Word symbias = *hash32++;
         Elf32_Word bitmask_nwords = *hash32++;
 
-        /* Must be a power of two.  */
-        assert((bitmask_nwords & (bitmask_nwords - 1)) == 0);
+        assert(IS_POWER_OF_2(bitmask_nwords));
         map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
         map->l_gnu_shift = *hash32++;
 

+ 2 - 4
Pal/src/host/Linux-SGX/db_files.c

@@ -125,8 +125,7 @@ static int64_t file_read(PAL_HANDLE handle, uint64_t offset, uint64_t count, voi
     if (offset >= total)
         return 0;
 
-    static_assert((TRUSTED_STUB_SIZE & (TRUSTED_STUB_SIZE - 1)) == 0,
-                  "TRUSTED_STUB_SIZE must be a power of two");
+    static_assert(IS_POWER_OF_2(TRUSTED_STUB_SIZE), "TRUSTED_STUB_SIZE must be a power of two");
 
     uint64_t end       = (offset + count > total) ? total : offset + count;
     uint64_t map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
@@ -236,8 +235,7 @@ static int file_map(PAL_HANDLE handle, void** addr, int prot, uint64_t offset, u
     uint64_t map_start, map_end;
 
     if (stubs) {
-        static_assert((TRUSTED_STUB_SIZE & (TRUSTED_STUB_SIZE - 1)) == 0,
-                      "TRUSTED_STUB_SIZE must be a power of two");
+        static_assert(IS_POWER_OF_2(TRUSTED_STUB_SIZE), "TRUSTED_STUB_SIZE must be a power of two");
         map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
         map_end   = (end + TRUSTED_STUB_SIZE - 1) & ~(TRUSTED_STUB_SIZE - 1);
     } else {

+ 1 - 1
Pal/src/host/Linux-SGX/sgx_main.c

@@ -254,7 +254,7 @@ int initialize_enclave (struct pal_enclave * enclave)
     }
 
     enclave->size = parse_int(cfgbuf);
-    if (enclave->size & (enclave->size - 1)) {
+    if (!IS_POWER_OF_2(enclave->size)) {
         SGX_DBG(DBG_E, "Enclave size not a power of two (an SGX-imposed requirement)\n");
         ret = -EINVAL;
         goto out;