Comments (4)
Good catch! want to send a PR correcting it or should we go ahead and fix it quick?
from cairo_native.
@azteca1998 thank you very much for the detailed response! I understand the flow much better now
from cairo_native.
@igaray actually, running this command by itself does not work out of the box. I had to write a wrapper C file to set it up. Is this intended?
For example, here is the command I ran for simple Cairo file compiled down to Sierra.
// program.cairo
fn main() -> u32 {
println!("hello world");
1
}
// wrapper.c
#include <stdint.h>
extern void alias_example_main(int64_t, __int128);
int main() {
int64_t arg0 = 0;
__int128 arg1 = 0;
alias_example_main(arg0, arg1);
return 0;
}
; program.ll
; added this line
@alias_example_main = alias { i64, i128, { i64, [24 x i8] } } (i64, i128), void (i64, i128)* @"example::example::main(f15270207278767002344)"
; main function definition
define { i64, i128, { i64, [24 x i8] } } @"example::example::main(f15270207278767002344)"(i64 %0, i128 %1) {
; ...
cairo-native-dump program.sierra -o program.mlir
mlir-opt \
--canonicalize \
--convert-scf-to-cf \
--canonicalize \
--cse \
--expand-strided-metadata \
--finalize-memref-to-llvm \
--convert-func-to-llvm \
--convert-index-to-llvm \
--reconcile-unrealized-casts \
"program.mlir" \
-o "program-llvm.mlir"
mlir-translate --mlir-to-llvmir program-llvm.mlir -o program.ll
# only this one is different from README.md
clang wrapper.c program.ll -Wno-override-module \
-L "$MLIR_SYS_180_PREFIX"/lib -L"./target/release/" \
-lcairo_native_runtime -lmlir_c_runner_utils \
-Wl,-rpath "$MLIR_SYS_180_PREFIX"/lib \
-Wl,-rpath ./target/release/ \
-o program
Even though I could compile the file, I still get a segfault 56807 segmentation fault ./program
.
from cairo_native.
actually, running this command by itself does not work out of the box. I had to write a wrapper C file to set it up. Is this intended?
It's not whether it's intended or not, but rather:
- Cairo always generates function names with their module, therefore it's impossible to have a
main
function. - Any non-trivial program will require gas and builtins, which do not exist in C, making the interface not work even if we solved the first problem.
As for your test program, try compiling it to Sierra and checking its function signature. In your case it's the following:
sample::sample::main@0([0]: RangeCheck, [1]: GasBuiltin) -> (RangeCheck, GasBuiltin, core::panics::PanicResult::<(core::integer::u32,)>);
As you can see the function needs RangeCheck
and GasBuiltin
, and returns (RangeCheck, GasBuiltin, core::panics::PanicResult::<(core::integer::u32,)>)
. You'd need something like this:
#include <stdio.h>
#include <stdint.h>
// Type consistency.
typedef __uint128_t uint128_t;
// Cairo Native types.
typedef struct __attribute__((aligned(16))) felt252_t
{
uint64_t data[4];
} felt252_t;
typedef struct native_array_t
{
void *data;
uint32_t start_offset;
uint32_t end_offset;
uint32_t capacity;
} native_array_t;
// Our function's return values (check out the C ABI for more info).
typedef struct cairo_main_t
{
uint64_t range_check;
uint128_t gas;
union {
struct { uint8_t tag; uint32_t value; } ok;
struct { uint8_t tag; native_array_t msg; } err;
} result;
} cairo_main_t;
// The weakref is the symbol name. You should make sure it matches the one in the MLIR output,
// including the `_mlir_ciface_` prefix.
static cairo_main_t *cairo_main(cairo_main_t *, uint64_t, uint128_t)
__attribute__((weakref("_mlir_ciface_program::program::main(f0)")));
int main()
{
cairo_main_t ret_data;
// You don't need to initialize the range check and gas into the return pointer, you can pass
// them directly too. The final builtin counter and gas will be stored in the structure, so
// having them originally there may make it easier to understand.
ret_data.range_check = 0;
ret_data.gas = 1000000000000;
cairo_main(&ret_data, ret_data.range_check, ret_data.gas);
// You can check either `ret_data.ok.tag` or `ret_data.err.tag`. They are the same field, but
// must be declared separately in each variant due to the way our enums work (if it was a common
// field, then the alignment of the payloads would be messed up).
if (ret_data.result.ok.tag == 0)
printf("cairo_main() returned Ok(%u)\n", ret_data.result.ok.value);
else
{
felt252_t *error_msg = (felt252_t *) ret_data.result.err.msg.data;
size_t i;
// Here's an example on how to read the arrays (for the error messages):
printf("cairo_main() returned an error:\n");
for (i = ret_data.result.err.msg.start_offset; i < ret_data.result.err.msg.end_offset; i++)
{
// Note that they're in reverse order. It's intentional because they're written in
// little-endian.
printf(
" %016lx%016lx%016lx%016lx\n",
error_msg[i].data[3],
error_msg[i].data[2],
error_msg[i].data[1],
error_msg[i].data[0]
);
}
}
return 0;
}
Disclaimer: The C program hasn't been tested.
from cairo_native.
Related Issues (20)
- Segmentation fault in tx 0x56b7a03200641d480abb3e314922e3adfa368d8b85a4dbf69e31845cf1ef1e4 HOT 2
- Segmentation fault in tx 0x74d05ea567909853f97889af773bf42f0655d5bccbd913c563e24638e829df8 HOT 1
- ARM bug: Failed to deserialize param #1
- u256_mul overflow in 0x00a1e8372b6de461e939b63d7d2a7c4a60bc333cae92a9e0800a575e13f202f7 HOT 4
- Bug: cairo_native swaps the high and low fields of `U256` compared to Cairo's `u256`
- Add Bravos transfer minimal contract test
- nullable snapshot_take is incorrectly implemented
- Error in tx 0x6c51758aa1ae9506602fffb9194da427fe948314b74eb93cdc9570558d4a88d, Killed process HOT 3
- Returning snapshot of dict causes double free HOT 2
- [BUG][Block 626173]Bug Native execution error: SM_SUB_UF HOT 2
- [BUG][Block 657887] `Native execution error: high !> low\n"` HOT 1
- [BUG][Block 657887] `Native execution error: Native execution error: Native execution error: DIRECTION\n` HOT 2
- [BUG][Block 656398] `OGD: Invalid signature` HOT 2
- Add error handling to JitValueWithInfoWrapper::to_bytes HOT 1
- [2.7.0] Implement bounded int libfuncs
- [2.7.0] Implement circuit libfuncs
- Improve doctests HOT 9
- Add Garaga test HOT 4
- Add contract-specific Executor with improved API
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cairo_native.