crashappsec / libcon4m

Base Compiler and Runtime Support for con4m
Apache License 2.0
0 stars 0 forks source link

`c4test` fails with `basic15.c4m` #48

Closed ee7 closed 3 weeks ago

ee7 commented 3 weeks ago

With:

and basic15.c4m:

https://github.com/crashappsec/libcon4m/blob/e9470ab7863c423e0729a5e26466153fa7f6685c/tests/basic15.c4m#L4-L15

the test fails, because it outputs 0 rather than 4:

$ debug/c4test tests/basic15.c4m
 FAIL: test /foo/libcon4m/tests/basic15.c4m: output mismatch.
Expected output
4
Actual
0

It looks like this is due to incorrect handling of hex literals, as the following diff makes the test pass:

-var x: uint = 0x0fffffffffffffff
+var x: uint = 1152921504606846975

That's the same number in decimal:

$ printf "%d\n" 0x0fffffffffffffff
1152921504606846975

Alternatively, this diff also works on my machine:

 """
 $output:
-4
+8
 """

 extern c4m_clz(u64) -> i32 {
   local: clz(x: uint) -> int
 }

-var x: uint = 0x0fffffffffffffff
+var x: uint = 0x00ffffffffffffff

 print(clz(x))

It looks like the problem is here:

../src/con4m/numbers.c:268:5: runtime error: load of value 94, which is not a valid value for type 'bool'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../src/con4m/numbers.c:268:5 in 
Conditional jump or move depends on uninitialised value(s)
   at 0x1751B8: i64_parse (numbers.c:268)
   by 0x197C49: c4m_parse_simple_lit (literals.c:268)
   by 0x1D85BD: simple_lit (parse.c:889)
   by 0x1DCA45: literal (parse.c:2571)
   by 0x1DE039: expression_start (parse.c:3172)
   by 0x1DF2D4: expression (parse.c:3727)
   by 0x1DB977: optional_initializer (parse.c:2038)
   by 0x1DBA3A: symbol_info (parse.c:2069)
   by 0x1DC063: variable_decl (parse.c:2277)
   by 0x1E047E: module (parse.c:4064)
   by 0x1E109F: c4m_parse (parse.c:4292)
   by 0x1CC93F: c4m_initial_load_one (compile.c:624)
 Uninitialised value was created by a stack allocation
   at 0x1750F8: i64_parse (numbers.c:267)

https://github.com/crashappsec/libcon4m/blob/e9470ab7863c423e0729a5e26466153fa7f6685c/src/con4m/numbers.c#L262-L269

viega commented 3 weeks ago

Yes, this feels like it's it. I pushed a fix, seems to work.