Intel® Software Guard Extensions (Intel® SGX)
Discussion board focused on hardware-based isolation and memory encryption to provide extended code protection in solutions.

Dynamic Memory Allocation

wendychao
Beginner
740 Views

Hey,

I'm trying to call the PBC library in SGX Enclave to implement dynamic memory allocation of a 2D array of type element_t in PBC for use by later programs. Unfortunately, encountered some unknown program exits, so I am already struggling with creating the correct code.

I have configured my SGX 2.0 environment after following the instructions sgx_emm and tested the sgx_mm API successfully. the configuration file is as follows

 

<EnclaveConfiguration>
  <ProdID>0</ProdID>
  <ISVSVN>0</ISVSVN>
  <TCSNum>3</TCSNum>
  <TCSMinPool>1</TCSMinPool>
  <TCSPolicy>1</TCSPolicy>
  <TCSMaxNum>3</TCSMaxNum>

  <StackMaxSize>0x1000000</StackMaxSize>
  <StackMinSize>0x10000</StackMinSize>
  <HeapMaxSize>0xF0000000</HeapMaxSize>
  <HeapInitSize>0x9000</HeapInitSize>
  <HeapMinSize>0x08000</HeapMinSize>
  <UserRegionSize>0x90000000</UserRegionSize>
  <DisableDebug>0</DisableDebug>
  <MiscSelect>1</MiscSelect>
  <MiscMask>0xFFFFFFFF</MiscMask>
</EnclaveConfiguration>

 

Now I need to call the PBC crypto library (already compiled lib_tpbc.a in /opt/intel/sgxsdk/lib64) in SGX Enclave to implement the dynamic memory allocation of a 2D array of type element_t, here is my function implementation.

 

//动态分配二维数组
void initialize_sets(int numSets, int sizeSet) 
{
    void *addr = 0;
    //const size_t ALLOC_SIZE = 0x10000000; // 256M
    size_t totalSize = numSets * sizeSet * sizeof(element_t);//1800B
    const size_t alignedSize = calculate_aligned_size(totalSize);

    int ret = sgx_mm_alloc(NULL, alignedSize, SGX_EMA_COMMIT_NOW, NULL, NULL, &addr);
    printf("sgx_mm_alloc = %d\n", ret); //0

    e_sets = (element_t **) (&addr);

    for (int i = 0; i < numSets; i++)
    {
        *(e_sets + i) = (element_t *)(addr + i * sizeSet * sizeof(element_t));
        for (int j = 0; j < sizeSet; j++)
        {
            element_init_Zr(e_sets[i][j], pairing);
            element_random(e_sets[i][j]);
            printf("initilize sets[%d][%d]\n", i, j);
        }
    }
}

 

Execute ./app  and the result is as follows:

wendychao_0-1693550682499.png

wendychao_1-1693550731137.png

My problem is that even though I call sgx_mm_alloc to allocate large enough memory dynamically, it still can't initialize e_sets correctly, e.g., it can allocate 256M of memory successfully, but when numSets =8, sizeSet=15, alignedSize=4KB, the function can only initialize the array up to e_sets[7][6] and exits, and it doesn't report an error, so it's hard for me to locate the error. I don't know what the problem is, please help me to solve this problem.

Thanks for your help very much!

0 Kudos
4 Replies
Iffa_Intel
Moderator
688 Views

Hi,


Could you share your hardware setup so that we can check the memory available in your hardware?

Sending us the logs would be beneficial as well.



Cordially,

Iffa


0 Kudos
wendychao
Beginner
676 Views

hi Iffa, 

 

Thanks for your response.

Here's some of my hardware setup info. 

root@ecsgx:~# lshw -short
H/W path      Device     Class          Description
===================================================
                         system         Alibaba Cloud ECS
/0                       bus            Motherboard
/0/400                   processor      Intel(R) Xeon(R) Platinum 8369B CPU @ 2.70GHz
/0/1000                  memory         4GiB System Memory
/0/1000/0                memory         4GiB DIMM RAM
/0/0                     memory         96KiB BIOS
/0/100                   bridge         440FX - 82441FX PMC [Natoma]
/0/100/1                 bridge         82371SB PIIX3 ISA [Natoma/Triton II]
/0/100/1.3               bridge         82371AB/EB/MB PIIX4 ACPI
/0/100/2                 display        GD 5446
/0/100/3                 communication  Virtio console
/0/100/3/0               generic        Virtual I/O device
/0/100/4                 storage        Virtio block device
/0/100/4/0    /dev/vda   disk           42GB Virtual I/O device
/0/100/4/0/1  /dev/vda1  volume         1023KiB BIOS Boot partition
/0/100/4/0/2  /dev/vda2  volume         190MiB Windows FAT volume
/0/100/4/0/3  /dev/vda3  volume         39GiB EXT4 volume
/0/100/5                 network        Virtio network device
/0/100/5/0    eth0       network        Ethernet interface
/0/100/6                 generic        Virtio memory balloon
/0/100/6/0               generic        Virtual I/O device
/0/1                     system         PnP device PNP0b00
/0/2                     input          PnP device PNP0303
/0/3                     input          PnP device PNP0f13
/0/4                     storage        PnP device PNP0700
/0/5                     communication  PnP device PNP0501

 

root@ecsgx:~# free -m
              总计         已用        空闲      共享    缓冲/缓存    可用
内存:        3746        2439         872          85         434         993
交换:           0           0           0
root@ecsgx:~# cat /proc/meminfo
MemTotal:        3836180 kB
MemFree:          888676 kB
MemAvailable:    1013912 kB
Buffers:           12172 kB
Cached:           391224 kB
SwapCached:            0 kB
Active:           184008 kB
Inactive:        2477356 kB
Active(anon):       4984 kB
Inactive(anon):  2349032 kB
Active(file):     179024 kB
Inactive(file):   128324 kB
Unevictable:       21336 kB
Mlocked:           18504 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Zswap:                 0 kB
Zswapped:              0 kB
Dirty:               444 kB
Writeback:             0 kB
AnonPages:       2279308 kB
Mapped:           320328 kB
Shmem:             87864 kB
KReclaimable:      42720 kB
Slab:             121240 kB
SReclaimable:      42720 kB
SUnreclaim:        78520 kB
KernelStack:       15364 kB
PageTables:        35768 kB
SecPageTables:         0 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     1918088 kB
Committed_AS:    9041968 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       66472 kB
VmallocChunk:          0 kB
Percpu:             1824 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      140568 kB
DirectMap2M:     2838528 kB
DirectMap1G:     1048576 kB

I am looking forward to your reply, thanks again!

 

Regards,

wendychao

 

0 Kudos
Iffa_Intel
Moderator
591 Views

Hi,


We require you to create a debug enclave and use gdb to set breakpoints in the enclave and verify everything is as it should be stepping through.


This is the best way to find the source of your issue & debug it.



Cordially,

Iffa

 


0 Kudos
Iffa_Intel
Moderator
439 Views

HI,


Thank you for your question. If you need any additional information from Intel, please submit a new question as Intel is no longer monitoring this thread. 


Cordially,

Iffa


0 Kudos
Reply