Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified LLVM crashes on nonsensical code #311

Closed
trasz opened this issue Jun 7, 2017 · 5 comments
Closed

Modified LLVM crashes on nonsensical code #311

trasz opened this issue Jun 7, 2017 · 5 comments
Assignees

Comments

@trasz
Copy link

trasz commented Jun 7, 2017

LLVM modified like this:

diff --git a/lib/Target/Mips/MipsSubtarget.h b/lib/Target/Mips/MipsSubtarget.h
index 01114d0fb60..cf0142e75a3 100644
--- a/lib/Target/Mips/MipsSubtarget.h
+++ b/lib/Target/Mips/MipsSubtarget.h
@@ -323,7 +323,7 @@ public:
   /// CHERI is not MIPSr6, but provides a hybrid implementation where
   /// in-cache-line unaligned accesses are handled in software and ones
   /// spanning cache lines are emulated in the OS.
-  bool systemSupportsUnalignedAccess() const { return IsCheri || hasMips32r6(); }
+  bool systemSupportsUnalignedAccess() const { return false; };
 
   // Set helper classes
   void setHelperClassesMips16();

... crashes on this:

// RUN: %clang_cc1 -triple cheri-unknown-freebsd -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name setlocale.c -mrelocation-model pic -pic-level 1 -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -target-cpu cheri -target-feature +soft-float -target-abi purecap -Wmips-cheri-prototypes -msoft-float -mfloat-abi soft -mllvm -mxgot -mllvm -mips-ssection-thre
shold=0 -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=gdb -coverage-notes-file /home/en322/cheri/build/cheribsd-obj-256/mips.mips64/usr/home/en322/cheri/cheribsd/lib/libc_cheri/setlocale.gcno -sys-header-deps -D FORCE_C_LOCALE -D FORCE_UTC_TZ -D __LP64__=1 -O2 -Wno-deprecated-declarations -Wno-cast-align -Werror=implicit-function-declaration -Wsystem-headers -Wall -Wno-format-y2
k -W -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -Wold-style-definition -Wno-pointer-sign -Wmissing-variable-declarations -Wthread-safety -Wno-empty-body -Wno-string-plus-int -Wno-unused-const-variable -Wno-incompatible-pointer
-types-discards-qualifiers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -std=gnu99 -fconst-strings -ferror-limit 19 -fmessage-length 0 -ftls-model=local-exec -fobjc-runtime=gnustep -fdiagnostics-show-option -vectorize-loops -vectorize-slp -cheri-linker -mllvm -mxmxgot -x c %s -o -
 | FileCheck %s
char *strcat();
a() { strcat(a, "/"); }

... like this:

llvm-lit: /home/en322/cheri/llvm/tools/clang/test/lit.cfg:200: note: using clang: '/usr/home/en322/cheri/build/llvm-256-build/./bin/clang'
-- Testing: 1 tests, 1 threads --
FAIL: Clang :: CodeGen/setlocale-7da148.creduce.test.c (1 of 1)
******************** TEST 'Clang :: CodeGen/setlocale-7da148.creduce.test.c' FAILED ********************
Script:
--
/usr/home/en322/cheri/build/llvm-256-build/./bin/clang -cc1 -internal-isystem /usr/home/en322/cheri/build/llvm-256-build/lib/clang/5.0.0/include -nostdsysteminc -triple cheri-unknown-freebsd -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name setlocale.c -mrelocation-model pic -pic-level 1 -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -target-cpu cheri 
-target-feature +soft-float -target-abi purecap -Wmips-cheri-prototypes -msoft-float -mfloat-abi soft -mllvm -mxgot -mllvm -mips-ssection-threshold=0 -dwarf-column-info -debug-info-kind=standalone -dwarf-version=2 -debugger-tuning=gdb -coverage-notes-file /home/en322/cheri/build/cheribsd-obj-256/mips.mips64/usr/home/en322/cheri/cheribsd/lib/libc_cheri/setlocale.gcno -sys-header-deps -D FORCE_C_LOCALE -D FORCE_UTC
_TZ -D __LP64__=1 -O2 -Wno-deprecated-declarations -Wno-cast-align -Werror=implicit-function-declaration -Wsystem-headers -Wall -Wno-format-y2k -W -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow -Wunused-parameter -Wcast-align -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls -Wold-style-definition -Wno-pointer-
sign -Wmissing-variable-declarations -Wthread-safety -Wno-empty-body -Wno-string-plus-int -Wno-unused-const-variable -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -std=gnu99 -fconst-strings -ferror-limit 19 -fmessage-length 0 -ftl
s-model=local-exec -fobjc-runtime=gnustep -fdiagnostics-show-option -vectorize-loops -vectorize-slp -cheri-linker -mllvm -mxmxgot -x c /usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c -o - | /usr/home/en322/cheri/build/llvm-256-build/./bin/FileCheck /usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c
--
Exit Code: 2

Command Output (stderr):
--
error: no check strings found with prefix 'CHECK:'
/usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c:2:13: warning: this function declaration is not a prototype [-Wstrict-prototypes]
char *strcat();
            ^
             void
/usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
a() { strcat(a, "/"); }
^
/usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c:3:14: warning: incompatible pointer types passing 'int ()' to parameter of type 'char * __capability' [-Wincompatible-pointer-types]
a() { strcat(a, "/"); }
             ^
/usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c:3:1: warning: no previous prototype for function 'a' [-Wmissing-prototypes]
a() { strcat(a, "/"); }
^
/usr/home/en322/cheri/llvm/tools/clang/test/CodeGen/setlocale-7da148.creduce.test.c:3:23: warning: control reaches end of non-void function [-Wreturn-type]
a() { strcat(a, "/"); }
                      ^
fatal error: error in backend: Cannot select: 0x805ca7410: iFATPTR = add 0x805ca4dd0, 0x805ca73a8
  0x805ca4dd0: iFATPTR = PTRADD 0x805ca4d00, Constant:i64<0>
    0x805ca4d00: iFATPTR = PTRADD 0x805ca41a0, 0x805ca4c30
      0x805ca41a0: iFATPTR = llvm.cheri.cap.offset.set TargetConstant:i64<17>, 0x805ca4068, 0x805ca7208
        0x805ca40d0: i64 = TargetConstant<17>
        0x805ca4068: iFATPTR = llvm.cheri.pcc.get TargetConstant:i64<30>
          0x805ca4000: i64 = TargetConstant<30>
        0x805ca7208: i64,ch = load<LD8[GOT]> 0x805c30438, 0x805ca71a0, undef:i64
          0x805ca71a0: i64 = MipsISD::Wrapper 0x805ca43a8, TargetGlobalAddress:i64<i32 ()* @a> 0 [TF=21]
            0x805ca43a8: i64 = add 0x805ca4340, Register:i64 %vreg0
              0x805ca4340: i64 = MipsISD::GotHi TargetGlobalAddress:i64<i32 ()* @a> 0 [TF=20]
                0x805ca4208: i64 = TargetGlobalAddress<i32 ()* @a> 0 [TF=20]
              0x805ca44e0: i64 = Register %vreg0
            0x805ca4ea0: i64 = TargetGlobalAddress<i32 ()* @a> 0 [TF=21]
          0x805ca4680: i64 = undef
      0x805ca4c30: i64,ch,glue = CopyFromReg 0x805ca4b60, Register:i64 %V0_64, 0x805ca4b60:1
        0x805ca4bc8: i64 = Register %V0_64
        0x805ca4b60: ch,glue = callseq_end 0x805ca4af8, TargetConstant:i64<0>, TargetConstant:i64<0>, 0x805ca4af8:1
          0x805ca4270: i64 = TargetConstant<0>
          0x805ca4270: i64 = TargetConstant<0>
          0x805ca4af8: ch,glue = MipsISD::CapJmpLink 0x805ca42d8, 0x805ca4888, Register:iFATPTR %C3, Register:i64 %GP_64, RegisterMask:Untyped, 0x805ca4a28:1
            0x805ca4888: iFATPTR = llvm.cheri.cap.offset.set Constant:i64<17>, 0x805ca4820, 0x805ca46e8
              0x805ca47b8: i64 = Constant<17>
              0x805ca4820: iFATPTR = llvm.cheri.pcc.get Constant:i64<30>
                0x805ca4750: i64 = Constant<30>
              0x805ca46e8: i64,ch = load<LD8[GlobalValueCallEntry]> 0x805ca42d8, 0x805ca4618, undef:i64
                0x805ca4618: i64 = MipsISD::Wrapper 0x805ca4548, TargetGlobalAddress:i64<i64 (i8 addrspace(200)*)* @strlen> 0 [TF=23]
                  0x805ca4548: i64 = add 0x805ca4478, Register:i64 %vreg0


                  0x805ca45b0: i64 = TargetGlobalAddress<i64 (i8 addrspace(200)*)* @strlen> 0 [TF=23]
                0x805ca4680: i64 = undef
            0x805ca48f0: iFATPTR = Register %C3
            0x805ca49c0: i64 = Register %GP_64
            0x805ca4a90: Untyped = RegisterMask
            0x805ca4a28: ch,glue = CopyToReg 0x805ca4958, Register:i64 %GP_64, Register:i64 %vreg0, 0x805ca4958:1
              0x805ca49c0: i64 = Register %GP_64
              0x805ca44e0: i64 = Register %vreg0
              0x805ca4958: ch,glue = CopyToReg 0x805ca42d8, Register:iFATPTR %C3, 0x805ca41a0
                0x805ca48f0: iFATPTR = Register %C3
                0x805ca41a0: iFATPTR = llvm.cheri.cap.offset.set TargetConstant:i64<17>, 0x805ca4068, 0x805ca7208
                  0x805ca40d0: i64 = TargetConstant<17>
                  0x805ca4068: iFATPTR = llvm.cheri.pcc.get TargetConstant:i64<30>

                  0x805ca7208: i64,ch = load<LD8[GOT]> 0x805c30438, 0x805ca71a0, undef:i64


    0x805ca4c98: i64 = Constant<0>
  0x805ca73a8: iFATPTR = INTTOPTR Constant:i64<1>
    0x805ca7340: i64 = Constant<1>
In function: a

--

********************
Testing Time: 0.33s
********************
Failing Tests (1):
    Clang :: CodeGen/setlocale-7da148.creduce.test.c

  Unexpected Failures: 1

Code: https://gist.github.com/trasz/bab434d029c8591da8dc4ec1d9f88605

@davidchisnall
Copy link
Member

As expected. A big part of the reason that we advertise supporting unaligned loads and stores for CHERI is that we don't have any of the code paths for handling unaligned loads and stores for CHERI operations (and we can't handle them efficiently, because we didn't define CHERI versions of load {word, doubleword} {right, left}.

@arichardson
Copy link
Member

This appears to be some strcat specific optimization because if you rename the function it no longer crashes.

@trasz
Copy link
Author

trasz commented Jun 7, 2017

There are at least two problems I can see:

  1. Without this change, it's kind of hard to make it work for the kernel code.

  2. This makes it somewhat hard to compare benchmark results: load left/right for plain MIPS, unaligned accesses for hybrid/purecap code.

@trasz trasz reopened this Jun 7, 2017
@trasz
Copy link
Author

trasz commented Jun 7, 2017

Ah, also, this is somehow related to the main problem I've been hitting, which is unaligned capabilities. This fixes my case my making the capabilities aligned... somehow.

@arichardson arichardson transferred this issue from CTSRD-CHERI/llvm Mar 13, 2019
@arichardson
Copy link
Member

Unaligned capability loads/stores have been handled correctly for quite a long time now, closing as fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants