pepper-project / pequin

A system for verifying outsourced computations, and applying SNARKs. Simplified release of the main Pepper codebase.
Other
122 stars 46 forks source link

Cannot access global variable in a function if it was redeclared in the calling function #53

Closed noresistence closed 5 years ago

noresistence commented 5 years ago

The following problem took me several days to track down.

When there is a variable declared in outer scope, say k, and this variable is redeclared in a function, and this function calls another function, which in turn accesses the global variable, then an Error occurs, indicating that probably the inner function tries to access the declaration of k from the calling function instead of the declaration of k from the global scope, which it should.

Example

The following example (based on one of the hashing examples in pepper) compiles with gcc -ansi -c -I. example.c but throws java.lang.RuntimeException: I don't know how to treat type uint bits 32 like a pointer in pequin.

example.h:

#include <stdint.h>

struct In {
    uint8_t input;
};

struct Out {
    uint8_t out;
};

static const uint32_t k[2] = {0x428a2f98,0x71374491};

example.c:

#include <example.h>

void sha256_transform() {
    uint32_t i, t;
    for (i = 0; i < 2; ++i) {
        t = k[i];
    }
}

void sha256_final() {
    sha256_transform();
}

void compute(struct In *input, struct Out *output) {
    uint32_t k;                               /* for-loop index variable */
    sha256_final();
}

compute() calls sha256_final() which in turn calls sha256_transform() which accesses the global k. But instead it accesses the variable k from the function scope of compute.

To my understanding, the code should compile without errors, but pequin's C Compiler throws I don't know how to treat type uint bits 32 like a pointer.

maxhowald commented 5 years ago

Thanks for tracking this down! Should be fixed now, thanks to @bjmnbraun!

noresistence commented 5 years ago

Thanks, the fix works for me when based on 8813e516cb2827e8a8752f7d025081fa9aefa161 (not with current master, as explained in #55)