Repository navigation
Closed
Description
LPython 0.13.0 generates valid C code from the following snippet, but segfaults when it is executed directly:
# file: main.py
from lpython import CPtr, i32, dataclass, c_p_pointer, Pointer, empty_c_void_p, p_c_pointer
from numpy import empty
@dataclass
class Foo:
x: i32
y: i32
def init(foos_ptr: CPtr) -> None:
foos: Pointer[Foo[1]] = c_p_pointer(foos_ptr, Foo[1])
foos[0] = Foo(1, 2)
def main() -> None:
foos: Foo[1] = empty(1, dtype=Foo)
foos_ptr: CPtr = empty_c_void_p()
p_c_pointer(foos, foos_ptr)
init(foos_ptr)
print("foos[0].x =", foos[0].x)
main()$ lpython --show-c main.py > main.c
$ clang -o app main.c
$ ./app
foos[0].x = 1
$ lpython main.py
Segmentation fault (core dumped)
// file: main.c
#include <complex.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <lfortran_intrinsics.h>
#define ASSERT(cond) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
exit(1); \
} \
}
#define ASSERT_MSG(cond, msg) \
{ \
if (!(cond)) { \
printf("%s%s", "ASSERT failed: ", __FILE__); \
printf("%s%s", "\nfunction ", __func__); \
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
printf("%s%s", #cond, "\n"); \
printf("%s", "ERROR MESSAGE:\n"); \
printf("%s%s", msg, "\n"); \
exit(1); \
} \
}
struct dimension_descriptor
{
int32_t lower_bound, length;
};
struct Foo {
int32_t x;
int32_t y;
};
struct xFoo
{
struct Foo *data;
struct dimension_descriptor dims[32];
int32_t n_dims;
bool is_allocated;
};
// Implementations
void init(void* foos_ptr)
{
struct xFoo foos_value;
struct xFoo* foos = &foos_value;
struct Foo foos_data[1];
foos->data = foos_data;
foos->n_dims = 1;
foos->dims[0].lower_bound = 0;
foos->dims[0].length = 1;
foos->n_dims = 1;
foos->dims[0].lower_bound = 0;
foos->dims[0].length = 1;
foos->data = (struct Foo*) foos_ptr;
foos->data[(0 - foos->dims[0].lower_bound)].x = 1;
foos->data[(0 - foos->dims[0].lower_bound)].y = 2;
}
void _xx_lcompilers_changed_main_xx()
{
struct xFoo foos_value;
struct xFoo* foos = &foos_value;
struct Foo foos_data[1];
foos->data = foos_data;
foos->n_dims = 1;
foos->dims[0].lower_bound = 0;
foos->dims[0].length = 1;
void* foos_ptr;
foos_ptr = (void*) foos->data;
init(foos_ptr);
printf("%s%s%d\n", "foos[0].x =", " ", foos->data[(0 - foos->dims[0].lower_bound)].x);
}
void _lpython_main_program()
{
_xx_lcompilers_changed_main_xx();
}
float _lfortran_caimag(float complex x);
double _lfortran_zaimag(double complex x);
float pi_32 = 3.14159265358979312e+00;
double pi_64 = 3.14159265358979312e+00;
double _lfortran_dacos(double x);
double _lfortran_dacosh(double x);
double _lfortran_dasin(double x);
double _lfortran_dasinh(double x);
double _lfortran_datan(double x);
double _lfortran_datanh(double x);
double _lfortran_dcos(double x);
double _lfortran_dcosh(double x);
double _lfortran_dexp(double x);
double _lfortran_dlog(double x);
double _lfortran_dlog10(double x);
double _lfortran_dsin(double x);
double _lfortran_dsinh(double x);
double _lfortran_dtan(double x);
double _lfortran_dtanh(double x);
float _lfortran_sacos(float x);
float _lfortran_sacosh(float x);
float _lfortran_sasin(float x);
float _lfortran_sasinh(float x);
float _lfortran_satan(float x);
float _lfortran_satanh(float x);
float _lfortran_scos(float x);
float _lfortran_scosh(float x);
float _lfortran_sexp(float x);
float _lfortran_slog(float x);
float _lfortran_slog10(float x);
float _lfortran_ssin(float x);
float _lfortran_ssinh(float x);
float _lfortran_stan(float x);
float _lfortran_stanh(float x);
int main(int argc, char* argv[])
{
_lpython_main_program();
return 0;
}Metadata
Metadata
Assignees
Labels
No labels
Activity
czgdp1807 commentedon May 16, 2023
Actually this is achieved by
CPtrToPointer(expr cptr, expr ptr, expr? shape)in ASR. Now, the same node is being used to representC_F_POINTERin Fortran. The difference between Python and Fortran is that Python's default lower bound for arrays is 0 but the same thing for Fortran is 1. Now,CPtrToPointersets the lower bound of the new array pointer as 1 (because it assumes that it got created fromC_F_POINTER). In the above example we need it to set the lower bound to 0 so that indexing works correctly. To do this I think we might need to do the following,shapeis absent butfptris an array. See https://gcc.gnu.org/onlinedocs/gfortran/C_005fF_005fPOINTER.html.shapewhenever its present in LLVM implementation.CPtrToPointernode in LPython, just never fillshapeand instead use the type ofptrto fill the lower bounds (which get set to 0 in LPython).The above is hackish by the way. Another clean approach is to just specify the lower bounds of each dimension in a new argument in
CPtrToPointer,CPtrToPointer(expr cptr, expr ptr, expr? shape, expr? lower_bounds).For fortran
lower_boundswill be an array constant of all 1s, for Python it will be 0s. So no hacks and everything comes out clean.What do you say @certik? Which approach to follow?